Saturday, May 19, 2012

.NET - Strings, Encodings

Notes:

1. string.Compare(stringA, stringB, boolIgnoreCase, CultureInfo)



 //the infamous Turkish I problem
            string a = "file";
            string b = "file";
            bool equalInvariant = string.Compare(a, b, true, CultureInfo.InvariantCulture) == 0;
            bool equalTurkish = string.Compare(a, b, true, CultureInfo.CreateSpecificCulture("tr-TR")) == 0;



2. stringA.ToUpper() ---Uses current culture
    stringA.ToUpperInvariant()
    stringA.ToUpper(CultureInfo.CreateSpecificCulture("tr-TR"))


3. stringA.Split(delimitersCharArray);


4.

void Main()
{
string name = "Manikandan";

var bytes = Encoding.ASCII.GetBytes(name);

Console.WriteLine ("ASCII: Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);


bytes = Encoding.Unicode.GetBytes(name);

Console.WriteLine ("Unicode Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF7.GetBytes(name);

Console.WriteLine ("UTF7 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF8.GetBytes(name);

Console.WriteLine ("UTF8 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF32.GetBytes(name);

Console.WriteLine ("UTF8 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

}

output:


ASCII: Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

Unicode Bytes: 4D-00-61-00-6E-00-69-00-6B-00-61-00-6E-00-64-00-61-00-6E-00
Length: 20

UTF7 Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

UTF8 Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

UTF8 Bytes: 4D-00-00-00-61-00-00-00-6E-00-00-00-69-00-00-00-6B-00-00-00-61-00-00-00-6E-00-00-00-64-00-00-00-61-00-00-00-6E-00-00-00
Length: 40

No comments:

Post a Comment