简体   繁体   中英

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 char s, 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.)

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

Use this:

str = str.Substring(0, 1);

or

str[0];

or

str.FirstOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM