#99 – Use StringInfo to Get Specific Characters From A UTF32 String

We saw that you cannot use the normal string index [] to get individual characters from a UTF32 string.  Instead, you need to use the System.Globalization.StringInfo class.

In the example below, we first get a list of indexes to each of the three characters in our UTF32 string.  We then extract index each character separately.

 s = "A𠈓C";
 int n = s.Length;     // 4, because of 4-byte character in middle

 // Get locations of text elements
 int[] indexes = StringInfo.ParseCombiningCharacters(s);  // 0, 1 and 3

 // Retrieve single element
 string nextChar = StringInfo.GetNextTextElement(s, 0);   // A
 nextChar = StringInfo.GetNextTextElement(s, 1);          // 𠈓
 nextChar = StringInfo.GetNextTextElement(s, 3);          // C
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: