Quantcast
Channel: 2,000 Things You Should Know About C# » Unicode
Viewing all articles
Browse latest Browse all 10

#1,006 – Getting the Length of a String

$
0
0

You can use the string.Length property to get an integer representing the length of a string.  In most cases, length means–the number of characters.

            // 3 Latin (ASCII) characters
            string simple = "abc";
            // 2 other characters: U+0100, E+4E01
            string other = "Ā丁";

            Console.WriteLine(string.Format("Length 1 = {0}", simple.Length));
            Console.WriteLine(string.Format("Length 2 = {0}", other.Length));

1006-001

 

It’s important to note that the Length property returns the number of individual Char objects that make up the string.  This can be different from the actual number of Unicode characters.


Filed under: Strings Tagged: C#, Length, Strings, Unicode

Viewing all articles
Browse latest Browse all 10

Trending Articles