Recursive AI

Split Unicode text into sentences 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.

Split at Unicode sentence boundaries, not at every period. No font file is needed.

Terminal
dotnet add package RecursiveAI.OneFont

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneFont;
 
string text = "It costs 3.50. 次です。 Done!";
var cuts = TextSegmentation
    .GetSentenceBoundaries(text);
for (int i = 1; i < cuts.Length; i++)
{
    var sentence = text[cuts[i - 1]..cuts[i]];
    Console.WriteLine($"[{sentence}]");
}

Unzip the example, then dotnet run.

Prints each sentence in brackets so you can see the spaces it keeps:

Text
[It costs 3.50. ]
[次です。 ]
[Done!]

An empty string gives one boundary (0), so the loop prints nothing. Each pair of boundaries marks a slice that includes its start and excludes its end.

Variations

Trim whitespace for display

Replace the loop with this version to drop surrounding spaces without dropping the punctuation:

C#
for (int i = 1; i < cuts.Length; i++)
{
    var sentence = text.AsSpan(
        cuts[i - 1], cuts[i] - cuts[i - 1]);
    Console.WriteLine($"[{sentence.Trim()}]");
}
Text
[It costs 3.50.]
[次です。]
[Done!]

The short answer

TextSegmentation.GetSentenceBoundaries(text) returns an int[] of UTF-16 offsets, starting at 0 and ending at text.Length. Slice the string between each pair of consecutive offsets to get the sentences, including the last one even if it has no ending punctuation.

How do I split a string into sentences and keep the punctuation?

A delimiter split cuts at characters you name. String.Split uses the separators you supply, so giving it a period tells it to split at the period in 3.50 too. OneFont instead follows the default Unicode sentence-boundary rules. In this example it leaves the decimal point alone, breaks after the period that ends the first sentence, and recognizes the Japanese 。 in the second. A sentence retains its trailing punctuation and whitespace.

The offsets count UTF-16 code units, just like C# string indexes. Use them with the same string you passed in. If you trim or otherwise edit the source before slicing, the offsets may point somewhere else. For a search result that has to highlight its source text, keep the original start and end offsets and trim only the copy you display.

Can I split sentences without breaking on abbreviations?

The default Unicode rules use context around a period, but they don’t consult a language-specific abbreviation list. An abbreviation followed by a capitalized word can still be split. If titles such as “Dr.” must always stay with the following name in your data, treat Unicode boundaries as candidates and apply your own rule for those cases. The API doesn’t accept an abbreviation list or a locale argument.

Test the cases that matter to your source material: decimals, titles, closing quotes, paragraph breaks, and a final sentence without an ending mark. If you need the displayed text to match the original punctuation and spacing exactly, keep the slices untrimmed.

All onefont recipes