---
title: "Find Unicode grapheme boundaries in C#"
url: https://recursiveai.org/onefont/cookbook/find-unicode-grapheme-boundaries/
description: "Find UTF-16 boundaries for combining marks, emoji ZWJ sequences and flags, then slice whole grapheme clusters with onefont in C#."
version: "1.4.0"
---

# Find Unicode grapheme boundaries in C#

**onefont** is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/find-unicode-grapheme-boundaries/find-unicode-grapheme-boundaries.zip)

Slice text at grapheme boundaries to keep combining accents and joined emoji together.

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onefont/cookbook/find-unicode-grapheme-boundaries/find-unicode-grapheme-boundaries.zip), then `dotnet run`.

Prints:

```text
0, 2, 7, 11
á
👩‍💻
🇺🇸
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.

```csharp
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:

```text
0, 2, 7, 11
á
👩‍💻
🇺🇸
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:

```csharp
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(""));
```

```text
0: 2
2: 5
7: 4
0
```

## Related recipes

[Measure and wrap text](https://recursiveai.org/onefont/cookbook/measure-and-wrap-text/)

---

## 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](https://learn.microsoft.com/en-us/dotnet/standard/base-types/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](https://recursiveai.org/onefont/docs/shaping/#line-breaking-and-graphemes).
