Supplementary Characters in C#

Inspired by my posting about supplementary characters in SQL Server 2012, I checked the capabilities of .Net to support supplementary characters:

string s143743 = "𣅿";
Console.WriteLine(new StringInfo(s143743).LengthInTextElements.ToString());
string s131518 = "𠆾𠇀𠇃";
Console.WriteLine(new StringInfo(s131518).LengthInTextElements.ToString());

The results are

Length: 2
LengthInTextElements: 1
Length: 6 
LengthInTextElements: 3

As you can see, the string.Length property returns the number of 16-bit words (= .Net char structure) the string contains. To handle supplementary characters correctly in .Net, you need to use the StringInfo class.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: