Slice text at grapheme boundaries to keep combining accents and joined emoji together.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; string text = "a\u0301👩💻🇺🇸"; int[] cuts = TextSegmentation .GetGraphemeBoundaries(text); Console.WriteLine(string.Join(", ", cuts)); for (int i = 0; i < cuts.Length - 1; i++) Console.WriteLine( text[cuts[i]..cuts[i + 1]]); int first = TextSegmentation .GetGraphemeLength(text); Console.WriteLine( $"First grapheme UTF-16 length: {first}");
Unzip the example, then dotnet run.
Prints:
0, 2, 7, 11 á 👩💻 🇺🇸 First grapheme UTF-16 length: 2
The full program
The input contains a plus combining acute accent, woman technologist joined by a zero width joiner, and a US flag pair. All input is in the program; no font files are required.
using OneFont; string text = "a\u0301👩💻🇺🇸"; int[] boundaries = TextSegmentation.GetGraphemeBoundaries(text); Console.WriteLine(string.Join(", ", boundaries)); for (int i = 0; i < boundaries.Length - 1; i++) Console.WriteLine(text[boundaries[i]..boundaries[i + 1]]); Console.WriteLine($"First grapheme UTF-16 length: {TextSegmentation.GetGraphemeLength(text)}");
dotnet run prints:
0, 2, 7, 11 á 👩💻 🇺🇸 First grapheme UTF-16 length: 2
Empty text returns the boundary array [0], and GetGraphemeLength returns 0.
Variations
Walk the same text without first building a boundary array:
using OneFont; string text = "a\u0301👩💻🇺🇸"; for (int offset = 0; offset < text.Length;) { int length = TextSegmentation.GetGraphemeLength(text.AsSpan(offset)); Console.WriteLine($"{offset}: {length}"); offset += length; } Console.WriteLine(TextSegmentation.GetGraphemeLength(""));
0: 2 2: 5 7: 4 0
Related recipes
The short answer
Call TextSegmentation.GetGraphemeBoundaries to get the UTF-16 start indexes of extended grapheme clusters, followed by the string’s length. Slice between consecutive indexes. Use GetGraphemeLength when you only need the length of the cluster at the beginning of a span.
The example returns 0, 2, 7, 11. These are offsets into a C# string, not Unicode scalar values or byte positions. The final 11 marks the end of the string; it is not the start of another grapheme. The number of clusters is therefore the number of boundaries minus one.
How do I find Unicode grapheme boundaries for emoji in C#?
The snippet at the top includes the whole input and prints each complete slice. Woman technologist contains two emoji characters joined by a zero width joiner, yet it is one extended grapheme cluster. The US flag is a regional indicator pair and also stays together. The first group is an ordinary letter followed by a combining accent.
The pinned library follows UAX #29 extended grapheme clusters, including combining marks, emoji ZWJ sequences and modifiers, flags, Hangul jamo and syllables, and Indic conjuncts. It uses Unicode 17.0 data. This makes the operation a segmentation step: no font load or glyph lookup is needed to decide these boundaries.
The variation walks the input from a known cluster boundary, obtains the next length and advances by that length. Keep that precondition when passing a sliced span to GetGraphemeLength: its documented input starts at a cluster boundary. Starting at an arbitrary code unit halfway through an emoji sequence cannot recover the boundary that came before it.
For empty text, the boundary array contains only zero and the first-cluster length is zero. A loop over adjacent boundaries naturally performs no slices. An unpaired surrogate is its own cluster under the length method’s documented behavior; segmentation does not turn malformed text into a valid Unicode scalar sequence.
What is a Unicode character in a C# string?
A C# char is a UTF-16 code unit. A Unicode scalar value may occupy one or two of those code units, and an extended grapheme cluster may contain several scalar values. Microsoft’s character encoding introduction explains these distinctions. Counting char values therefore answers a different question from counting grapheme clusters.
For this string, the accent group occupies two UTF-16 code units, the joined emoji occupies five, and the flag occupies four. A byte count adds another encoding-dependent measurement; none of these offsets should be used directly as byte offsets into an encoded file.
A grapheme count is not a shaped glyph count or a display width. Fonts can combine letters into a ligature or use multiple glyphs for one cluster. Segmentation alone does not measure a caret position in pixels, decide how wide an emoji appears, or implement line wrapping. Use grapheme boundaries to keep text groups intact; use shaping and measurement when your task depends on the selected font and its metrics.
Source: onefont grapheme documentation.