Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 758
hippietrail Avatar asked Dec 25 '22 20:12

hippietrail


2 Answers

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);
like image 129
Sameer Avatar answered Dec 30 '22 10:12

Sameer


Use this:

str = str.Substring(0, 1);

or

str[0];

or

str.FirstOrDefault();
like image 33
Iswanto San Avatar answered Dec 30 '22 10:12

Iswanto San