Count an accented letter or a joined emoji as one character by counting grapheme clusters.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; string text = "e\u0301👩💻🇺🇸"; var edges = TextSegmentation .GetGraphemeBoundaries(text); Console.WriteLine(edges.Length - 1);
Unzip the example, then dotnet run.
Prints:
3The full program
The input is in the code: e plus a combining acute accent, followed by a joined emoji and a flag. No font file is needed. The program prints the three groups and the UTF-16 offsets that separate them.
using OneFont; string text = "e\u0301👩💻🇺🇸"; int[] edges = TextSegmentation.GetGraphemeBoundaries(text); Console.WriteLine($"Graphemes: {edges.Length - 1}"); Console.WriteLine($"UTF-16 code units: {text.Length}"); Console.WriteLine($"Boundaries: {string.Join(", ", edges)}"); for (int i = 0; i < edges.Length - 1; i++) Console.WriteLine(text[edges[i]..edges[i + 1]]);
Graphemes: 3 UTF-16 code units: 11 Boundaries: 0, 2, 7, 11 é 👩💻 🇺🇸
Empty input returns one boundary (0), so the same subtraction gives a count of zero.
Variations
Keep at most two characters without splitting an emoji
using OneFont; string text = "e\u0301👩💻🇺🇸"; var edges = TextSegmentation .GetGraphemeBoundaries(text); int count = Math.Min(2, edges.Length - 1); Console.WriteLine(text[..edges[count]]);
é👩💻Related recipes
The short answer
Call TextSegmentation.GetGraphemeBoundaries(text) and subtract one from the array’s length. The array holds the start of each grapheme cluster and one extra offset for the end of the string. Use this count for a character limit that should treat joined emoji and letters with combining marks as units.
How do I count visible characters in a C# string?
Unicode calls the groups in this example extended grapheme clusters. They’re a default approximation for what a reader treats as one character, not a count of drawn glyphs. The bundled program counts three: é, 👩💻, and 🇺🇸. A font may display a cluster using more than one glyph, but that doesn’t change the Unicode count. onefont applies the Unicode text segmentation rules with Unicode 17 data, as documented in the pinned README.
The returned offsets refer to the original string and can be used directly as C# slice indexes. For example, the second cluster starts at 2 and ends just before 7. To keep the first two clusters, the variation uses the boundary at index 2 to cut at UTF-16 offset 7. Cutting at offset 2 instead would keep only the accented letter. If your limit is defined in bytes or UTF-16 code units rather than user-perceived characters, use that unit for the limit; counting graphemes won’t tell you how much encoded storage you need.
Why does string.Length count emoji differently?
string.Length counts UTF-16 code units. In this example, the accent adds a code unit to e, the joined emoji occupies five code units (two for each emoji plus the zero-width joiner), and the flag occupies four. The total is eleven even though there are only three clusters. Counting Unicode scalar values instead still counts the combining mark, joiner, and regional indicators separately. The Microsoft guide to character encoding explains the three levels: code units, scalar values, and grapheme clusters.