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(s143743.Length.ToString());
Console.WriteLine(new StringInfo(s143743).LengthInTextElements.ToString());
string s131518 = "𠆾𠇀𠇃";
Console.WriteLine(s131518.Length.ToString());
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.

1 thought on “Supplementary Characters in C#

  1. Pingback: Length of UTF-8 VARCHAR in SQL Server | devioblog

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.