Strings

suggest change

Remarks

A .NET string (System.String) is a sequence of System.Char values, and each char is one UTF-16 code unit. That is not the same as a user-visible character.

A user-visible character is a grapheme, drawn as a glyph, and defined by one or more Unicode code points. Each code point is encoded as one or more code units. A single System.Char is therefore not always one grapheme:

There are many more issues in text handling. See How can I perform a Unicode-aware character-by-character comparison?.

For international text, enumerate text elements instead of char values. That avoids splitting a surrogate pair or a combining sequence:

public static class StringExtensions
{
    public static IEnumerable<string> EnumerateCharacters(this string s)
    {
        if (s == null)
            return Enumerable.Empty<string>();

        var enumerator = StringInfo.GetTextElementEnumerator(s.Normalize());
        while (enumerator.MoveNext())
            yield return (string)enumerator.Value;
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents