Count characters

suggest change

If you need to count characters then, for the reasons explained in Remarks section, you can’t simply use Length property because it’s the length of the array of System.Char which are not characters but code-units (not Unicode code-points nor graphemes). Correct code is then:

int length = text.EnumerateCharacters().Count();

A small optimization may rewrite EnumerateCharacters() extension method specifically for this purpose:

public static class StringExtensions
    public static int CountCharacters(this string text)
        if (String.IsNullOrEmpty(text))
            return 0;

        int count = 0;
        var enumerator = StringInfo.GetTextElementEnumerator(text);
        while (enumerator.MoveNext())

        return count;

Feedback about page:

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

Table Of Contents