Home > Programming > Some notes for handling string

Some notes for handling string

July 16th, 2010

Clearing my phone since it is very messy with a lot of files, and found some notes here:

Handling string in C#
List of Numeric Formats

* C or C – For Currency. Uses the cultures currency symbol.
* D or D – Integer types. Add a number for 0 padding eg D5.
* E or e – Scientific notation.
* F or f – Fixed Point.
* G or g – Compact fixed-point/scientific notation.
* N or n – Number. This can be enhanced by a NumberFormatInfo object.
* P or p – Percentage.
* R or r – Round-trip. Keeps exact digits when converted to string and back.
* X or x – Hexadecimal. x – uses abcdef, X use ABCDEF.

Dates can also be specified either using Standard Format strings

* O or o – YYYY-MM-dd:mm:ss:ffffffffzz
* R or r – RFC1123 eg ddd, dd MMM yyyy HH:ss GMT
* s – sortable . yyyy-MM-ddTHH:mm:ss
* u – Universal Sort Date – yyyy-MM-dd HH:mm:ssZ

or Format specifiers. There are too many of those to list here. Example:

using System;
using System.Text;
using System.Globalization;
 
namespace ex6
{
    class Program
    {
        static void Main(string[] args)
        {
            // Numeric Formatting Examples
            // integer
            int i = 5678;
            string s = string.Format("{0,10:D}", i);  // Into a string right aligned 10 width
            Console.WriteLine("{0,10:D}", i);         // or output direct    
            Console.WriteLine("{0,10:D7}", i);         // or output direct with leading 0
 
            // double and currency in various formats
            double d=47.5;
            double bigd = 19876543.6754;
            Console.WriteLine("{0,15:C2}", d);  //  In Uk = £47.50 right aligned in 15 width
 
            Console.WriteLine("{0,15:N10}", bigd); //  Number 
            Console.WriteLine("{0,15:E3}", d);  //  Scientific 4.750E+001
            Console.WriteLine("{0,15:F5}", d);  //  Fixed Point 47.50000
            Console.WriteLine("{0,15:G4}", d);  //  Compact 47.5
            Console.WriteLine("{0,10:P2}", d/100.0);  //  %
            Console.WriteLine("{0,15:R}", bigd); //  Roundtrip - not a digit lost
 
            // Hex-a-diddly-decimal
            Console.WriteLine("{0,10:x8}", i);   // lowercase 0000162e
            Console.WriteLine("{0,10:X8}", i);   // uppercase 0000162E
 
 
            // Date formats
            DateTime dt = DateTime.Now;
            // Standards
            Console.WriteLine("{0:O}", dt);   // O or o  yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzz
            Console.WriteLine("{0:R}", dt);   // R or r ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
            Console.WriteLine("{0:s}", dt);   // s yyyy'-'MM'-'dd'T'HH':'mm':'ss
            Console.WriteLine("{0:u}", dt);   // u yyyy'-'MM'-'dd HH':'mm':'ss'Z'
 
            // Using date/time specifiers
            Console.WriteLine("{0:t}", dt);  // short time
            Console.WriteLine("{0:T}", dt);  // long time
            Console.WriteLine("{0:d}", dt);  // short date 
            Console.WriteLine("{0:D}", dt);  // long date 
            Console.WriteLine("{0:f}", dt);  // long date / short time 
            Console.WriteLine("{0:F}", dt);  // long date / long time
            Console.WriteLine("{0:g}", dt);  // short date / short time 
            Console.WriteLine("{0:G}", dt);  // short date / long time
            Console.WriteLine("{0:o}", dt);  // Round Trip
 
            // roll your own... 
            Console.WriteLine("{0:dd/mm/yyyy HH:MM:ss}", dt);  // custom - what most people use (UK)!
            Console.WriteLine("{0:mm/dd/yyyy HH:MM:ss}", dt);  // custom - what most people use (US)!
            Console.WriteLine("{0:yyyy/mm/dd HH:MM:ss}", dt);  // custom - (Japan) Good for sorting!
 
            Console.WriteLine("{0:dd MMM yyyy HH:MM:ss}", dt);  // custom - month (UK)
            Console.WriteLine("{0:MMM dd yyyy HH:MM:ss}", dt);  // custom - month (US)
            Console.WriteLine("{0:yyyy MMM dd HH:MM:ss}", dt);  // custom - (Japan) 
            Console.ReadKey();
        }
    }
}

C:
Standard: \r\n
(don’t use \n\r)

Author: SquallLTT Categories: Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.


You can use the following emoticons:

:) ;) :D :)) :P :( B-) :X :-* :blushing: @};- >:D< more »