C#: how to get first character of a string?

hippietrail picture hippietrail · Apr 24, 2015 · Viewed 17.7k times · Source

We already have a question about getting the first 16-bit char of a string.

This includes the question code:

MyString.ToCharArray[0]

and accepted answer code:

MyString[0]

I guess there are some uses for that, but when the string contains text we hopefully are all aware that a single 16-bit char cannot hold a character, even in the restricted sense where we actually mean "codepoint".

I am a programmer but not a C# programmer. I am just trying to help an online colleague fix such a bug, in case you feel this is too basic a question.

So if we have a string in C# in a char array, encoded in correct UTF-16, possibly including a surrogate pair as the first character/codepoint and thus potentially consisting of two chars, how do I get that first character?

(I naïvely assume Microsoft provides a string function for this and that I don't have to manually check for surrogate pairs.)

Answer

Sameer picture Sameer · Apr 24, 2015

You can use StringInfo class which is aware of surrogate pairs and multibyte chars.

        var yourstring = "😂test"; // First char is multibyte char.
        var firstCharOfString = StringInfo.GetNextTextElement(yourstring,0);