---
title: "Find Unicode word boundaries in C#"
url: https://recursiveai.org/onefont/cookbook/find-unicode-word-boundaries/
description: "Word segmentation finds text ranges while preserving spaces, punctuation and their original positions. Runnable C# example with onefont."
version: "1.4.0"
---

# Find Unicode word 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-word-boundaries/find-unicode-word-boundaries.zip)

Word segmentation finds text ranges while preserving spaces, punctuation and their original positions.

```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;

int[] cuts = TextSegmentation
    .GetWordBoundaries("can't 3.14 中文");
Console.WriteLine(string.Join(", ", cuts));
```

Unzip [the example](https://recursiveai.org/onefont/cookbook/find-unicode-word-boundaries/find-unicode-word-boundaries.zip), then `dotnet run`.

Prints:

```text
0, 5, 6, 10, 11, 12, 13
```

## The full program

All input text is in the program; no font file is needed.

```csharp
using OneFont;

string text = "can't 3.14 中文";
int[] cuts = TextSegmentation
    .GetWordBoundaries(text);
Console.WriteLine(string.Join(", ", cuts));
for (int i = 0; i < cuts.Length - 1; i++)
    Console.WriteLine(
        $"[{text[cuts[i]..cuts[i + 1]]}]");
```

`dotnet run` prints:

```text
0, 5, 6, 10, 11, 12, 13
[can't]
[ ]
[3.14]
[ ]
[中]
[文]
```

Empty text returns `[0]`.

## Variations

Mark each boundary in a caller-owned byte buffer. A `FillWordStarts` buffer shorter than the text plus one throws `ArgumentException`:

```csharp
using OneFont;

string text = "can't 3.14 中文";
byte[] starts = new byte[text.Length + 1];
TextSegmentation.FillWordStarts(text, starts);
for (int i = 0; i < starts.Length; i++)
    if (starts[i] == 1)
        Console.WriteLine(i);
```

```text
0
5
6
10
11
12
13
```

## Related recipes

[Find Unicode sentence boundaries](https://recursiveai.org/onefont/cookbook/find-unicode-sentence-boundaries/)

[Find Unicode grapheme boundaries](https://recursiveai.org/onefont/cookbook/find-unicode-grapheme-boundaries/)

---

## The short answer

Use `TextSegmentation.GetWordBoundaries(text)` for Unicode word segmentation in C#. It returns UTF-16 indexes including the start and end of the input. Slice between neighboring indexes to preserve every character. The result includes whitespace and punctuation segments; it is not a list containing only words.

onefont follows the default Unicode 17 word-boundary rules in [UAX #29](https://www.unicode.org/reports/tr29/). No font file is involved. The same text has the same boundaries regardless of the typeface you later use to draw it.

## What is a word boundary?

A boundary is an offset between segments, rather than a character to remove. In the example, `can't` occupies indexes 0 through 4; the boundary at 5 is just after the apostrophe-containing word. The next segment is the space. The decimal `3.14` stays together too, rather than breaking at its point.

Keep the terminal offset when slicing. It closes the final segment, and does not introduce an extra empty word. Empty text returns `[0]`, so the loop in the full program performs no iterations. These are C# string offsets, not UTF-8 byte offsets. Positions inside a surrogate pair are never boundaries.

## How do you get a list of Unicode word boundaries in C#?

Use the integer array for slicing, as the complete program does. Use `FillWordStarts` from the variation when the rest of your application needs a boundary flag at each string offset instead. Its byte buffer must contain at least `text.Length + 1` entries, including the end marker. It writes 1 at boundaries and 0 within segments; entries beyond that required range remain untouched.

Both forms describe the same cuts. Neither discards spaces, punctuation or symbols. For word counting or indexing, decide which segments your application counts as words before using the number of segments as a word count. An editor selecting text may need punctuation segments that a search index would discard.

The Chinese characters in the example become individual segments. That demonstrates default boundaries, not a dictionary choosing complete Chinese words. The library applies no language-specific tailoring. If your application needs dictionary tokenization, the boundary array alone does not provide it.

## Is a Unicode word boundary the same as a regex word boundary?

No. A conventional regex word boundary depends on the transition between a word character and a non-word character. Unicode’s default word segmentation uses contextual rules, including the handling of apostrophes and numbers. See [Microsoft’s description of word characters in .NET regular expressions](https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#word-character-w).

Choose this API when you need segments that retain their original string positions and follow UAX #29. Do not treat its returned positions as interchangeable with regex `\b` matches. A whole-word search needs both a match and a decision about which segment boundaries to accept.

Word boundaries also differ from line breaks and grapheme boundaries. Graphemes keep a combining accent or joined emoji together; line breaks identify possible wrapping positions. Use the boundary type that matches the operation, rather than using word segments to truncate a single visible character or to fit a line to a measured width.
