C# grapheme
last modified November 15, 2026
This article shows how to work with graphemes, code points, and surrogate pairs of Unicode strings in C#.
Graphemes and Unicode
A grapheme is the smallest unit of a writing system: what a reader perceives as a single character. A grapheme may be a single letter, a letter with accents, a syllable of an Indic script, or an emoji built from several components.
Unicode is the standard that assigns a number, a
code point, to every character of the world's writing systems.
Code points are written as U+ followed by hexadecimal digits;
the Latin letter Q is U+0051 and the Cyrillic small letter
zhe (ж) is U+0436. Some graphemes are a single code
point, others are a grapheme cluster of several code points. The
Hindi syllable ते consists of the letter ta (U+0924)
and the vowel sign e (U+0947).
| Code point | Character | Name |
|---|---|---|
| U+0041 | A | LATIN CAPITAL LETTER A |
| U+03B1 | α | GREEK SMALL LETTER ALPHA |
| U+0416 | Ж | CYRILLIC CAPITAL LETTER ZHE |
| U+05D0 | א | HEBREW LETTER ALEF |
| U+0924 | त | DEVANAGARI LETTER TA |
| U+1F600 | 😀 | GRINNING FACE |
To store text, code points are encoded into code units by an encoding: UTF-8 uses one to four bytes per code point, UTF-16 one or two 16-bit units, and UTF-32 one 32-bit unit.
Strings in .NET
A .NET string is a sequence of UTF-16 code units, and a
char is one code unit. Code points from U+0000 to
U+FFFF, the Basic Multilingual Plane (BMP), fit into one
char. Code points above U+FFFF, such as most
emoji, need two char values called a surrogate pair:
a high surrogate from the range D800–DBFF
followed by a low surrogate from DC00–DFFF.
.NET therefore has three levels of text units:
char: a UTF-16 code unit;string.Lengthcounts these,Rune: a Unicode scalar value, i.e. a code point; enumerated withstring.EnumerateRunes,- text element: a grapheme cluster; handled by
StringInfoandTextElementEnumeratorfromSystem.Globalization.
Since .NET 5, text elements follow the extended grapheme cluster rules of Unicode Standard Annex #29, so they correspond to user-perceived characters.
C# counting graphemes
The length of a string depends on what is counted. The first example counts the UTF-16 code units, code points, UTF-8 bytes, and graphemes of several strings.
using System.Globalization;
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
string[] words =
[
"falcon",
"caf\u00E9",
"cafe\u0301",
"\u0928\u092E\u0938\u094D\u0924\u0947",
"\U0001F600",
"\U0001F44D\U0001F3FD",
"\U0001F468\u200D\U0001F469\u200D\U0001F467",
"\U0001F1F8\U0001F1F0"
];
Console.WriteLine($"{"text",-8} {"chars",5} {"runes",5} {"bytes",5} {"graphemes",9}");
foreach (string word in words)
{
int chars = word.Length;
int runes = word.EnumerateRunes().Count();
int bytes = Encoding.UTF8.GetByteCount(word);
int graphemes = new StringInfo(word).LengthInTextElements;
Console.WriteLine($"{word,-8} {chars,5} {runes,5} {bytes,5} {graphemes,9}");
}
The non-ASCII characters are written as escape sequences:
\u with four hexadecimal digits for BMP code points and
\U with eight digits for any code point.
"caf\u00E9", "cafe\u0301",
The word café in two forms: with the precomposed letter
U+00E9, and as the letter e followed by the combining acute
accent U+0301. Both display the same.
"\U0001F44D\U0001F3FD", "\U0001F468\u200D\U0001F469\u200D\U0001F467", "\U0001F1F8\U0001F1F0"
A thumbs up with a skin tone modifier, a family joined by zero-width joiners
(U+200D), and the flag of Slovakia made of two regional
indicator symbols. Each is a single grapheme.
int chars = word.Length; int runes = word.EnumerateRunes().Count(); int bytes = Encoding.UTF8.GetByteCount(word); int graphemes = new StringInfo(word).LengthInTextElements;
Length counts UTF-16 code units, the rune count is the number
of code points, GetByteCount returns the size in UTF-8, and
LengthInTextElements counts graphemes. Only the last number
matches what a reader sees. The columns of the output are misaligned
because the padding counts char values, not display width.
$ dotnet run text chars runes bytes graphemes falcon 6 6 6 6 café 4 4 5 4 café 5 5 6 4 नमस्ते 6 6 18 4 😀 2 1 4 1 👍🏽 4 2 8 1 👨👩👧 8 5 18 1 🇸🇰 4 2 8 1
C# code points, bytes, and graphemes
In the next example, we take apart the Hindi word namaste.
using System.Globalization;
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
string word = "\u0928\u092E\u0938\u094D\u0924\u0947";
Console.WriteLine($"The Hindi word namaste: {word}");
Console.WriteLine("Code points:");
foreach (Rune rune in word.EnumerateRunes())
{
UnicodeCategory cat = Rune.GetUnicodeCategory(rune);
Console.WriteLine($" U+{rune.Value:X4} {cat}");
}
byte[] bytes = Encoding.UTF8.GetBytes(word);
Console.WriteLine($"UTF-8 bytes: {Convert.ToHexString(bytes)}");
Console.WriteLine("Graphemes:");
TextElementEnumerator e = StringInfo.GetTextElementEnumerator(word);
while (e.MoveNext())
{
string grapheme = e.GetTextElement();
Console.WriteLine($" {grapheme} ({grapheme.Length} chars)");
}
We print the code points, the UTF-8 bytes, and the graphemes of the word.
Console.OutputEncoding = Encoding.UTF8;
On Windows, the console uses a legacy code page by default; we set UTF-8 to display non-ASCII characters correctly.
foreach (Rune rune in word.EnumerateRunes())
{
UnicodeCategory cat = Rune.GetUnicodeCategory(rune);
Console.WriteLine($" U+{rune.Value:X4} {cat}");
}
EnumerateRunes decodes the code points, combining surrogate
pairs into a single Rune. We print each code point with its
Unicode category. Two of them are non-spacing marks: the virama, which
suppresses the inherent vowel, and the vowel sign e. These combine with the
preceding letters.
byte[] bytes = Encoding.UTF8.GetBytes(word);
Console.WriteLine($"UTF-8 bytes: {Convert.ToHexString(bytes)}");
Devanagari code points take three bytes each in UTF-8, so the six code points take 18 bytes.
TextElementEnumerator e = StringInfo.GetTextElementEnumerator(word);
while (e.MoveNext())
{
string grapheme = e.GetTextElement();
Console.WriteLine($" {grapheme} ({grapheme.Length} chars)");
}
The TextElementEnumerator goes over the graphemes. The marks
are joined with the preceding letters, so the word has four graphemes.
$ dotnet run The Hindi word namaste: नमस्ते Code points: U+0928 OtherLetter U+092E OtherLetter U+0938 OtherLetter U+094D NonSpacingMark U+0924 OtherLetter U+0947 NonSpacingMark UTF-8 bytes: E0A4A8E0A4AEE0A4B8E0A58DE0A4A4E0A587 Graphemes: न (1 chars) म (1 chars) स् (2 chars) ते (2 chars)
C# surrogate pairs
The char and Rune types provide methods for
working with surrogate pairs.
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
string[] emojis = ["\U0001F600", "\U0001F680", "\U0001F47B"];
foreach (string emoji in emojis)
{
char high = emoji[0];
char low = emoji[1];
Console.WriteLine($"{emoji} U+{char.ConvertToUtf32(high, low):X} "
+ $"high: {(int)high:X4} {char.IsHighSurrogate(high)} "
+ $"low: {(int)low:X4} {char.IsLowSurrogate(low)}");
}
Rune rocket = new(0x1F680);
Console.WriteLine($"{rocket}: {rocket.Utf16SequenceLength} UTF-16 units, "
+ $"{rocket.Utf8SequenceLength} UTF-8 bytes, BMP: {rocket.IsBmp}");
string s = char.ConvertFromUtf32(0x1F47B);
Console.WriteLine($"{s} has Length {s.Length}");
We inspect the surrogate pairs of three emoji.
char high = emoji[0]; char low = emoji[1];
Each emoji is stored as two char values.
Console.WriteLine($"{emoji} U+{char.ConvertToUtf32(high, low):X} "
char.ConvertToUtf32 combines a surrogate pair into the code
point. IsHighSurrogate and IsLowSurrogate check
the ranges of the two units.
Rune rocket = new(0x1F680);
A Rune is created from a code point. It knows how many UTF-16
units and UTF-8 bytes it needs and whether it lies in the BMP.
string s = char.ConvertFromUtf32(0x1F47B);
char.ConvertFromUtf32 converts a code point to a string, which
has two char values here.
$ dotnet run 😀 U+1F600 high: D83D True low: DE00 True 🚀 U+1F680 high: D83D True low: DE80 True 👻 U+1F47B high: D83D True low: DC7B True 🚀: 2 UTF-16 units, 4 UTF-8 bytes, BMP: False 👻 has Length 2
C# truncating and reversing strings
Methods that work with char indexes, such as
Substring or reversing an array of characters, can split
graphemes and even surrogate pairs.
using System.Globalization;
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
string text = "I \u2764\uFE0F C# \U0001F44D\U0001F3FD and .NET";
Console.WriteLine($"Substring(0, 9): [{text.Substring(0, 9)}]");
Console.WriteLine($"Substring(0, 10): [{text.Substring(0, 10)}]");
Console.WriteLine($"Truncate(8): [{Truncate(text, 8)}]");
Console.WriteLine($"Naive reverse: [{new string(text.Reverse().ToArray())}]");
Console.WriteLine($"Grapheme-aware: [{ReverseGraphemes(text)}]");
static string Truncate(string s, int maxGraphemes)
{
int length = 0;
for (int i = 0; i < maxGraphemes && length < s.Length; i++)
{
length += StringInfo.GetNextTextElementLength(s.AsSpan(length));
}
return s[..length];
}
static string ReverseGraphemes(string s)
{
var parts = new List<string>();
var e = StringInfo.GetTextElementEnumerator(s);
while (e.MoveNext())
{
parts.Add(e.GetTextElement());
}
parts.Reverse();
return string.Concat(parts);
}
We cut and reverse a string with an emoji, first naively, then with grapheme-aware helpers.
string text = "I \u2764\uFE0F C# \U0001F44D\U0001F3FD and .NET";
The heart is U+2764 followed by the variation selector
U+FE0F, which requests the emoji presentation. The thumbs up
has a skin tone modifier; it takes four char values.
Console.WriteLine($"Substring(0, 9): [{text.Substring(0, 9)}]");
Console.WriteLine($"Substring(0, 10): [{text.Substring(0, 10)}]");
Nine characters end with a lone high surrogate, which is printed as the replacement character. Ten characters keep the surrogate pair but drop the skin tone modifier.
for (int i = 0; i < maxGraphemes && length < s.Length; i++)
{
length += StringInfo.GetNextTextElementLength(s.AsSpan(length));
}
The Truncate helper adds up the lengths of whole graphemes.
GetNextTextElementLength, available since .NET 5, returns the
length of the grapheme at the start of a span.
Console.WriteLine($"Naive reverse: [{new string(text.Reverse().ToArray())}]");
Reversing the char values swaps the halves of the surrogate
pairs and moves the marks before their base characters. The low surrogate of
the thumb and the high surrogate of the skin tone even form a new, unrelated
code point, a domino tile.
parts.Reverse(); return string.Concat(parts);
The grapheme-aware version collects the text elements in a list, reverses the list, and joins it.
$ dotnet run Substring(0, 9): [I ❤️ C# �] Substring(0, 10): [I ❤️ C# 👍] Truncate(8): [I ❤️ C# 👍🏽] Naive reverse: [TEN. dna �🁍� #C ️❤ I] Grapheme-aware: [TEN. dna 👍🏽 #C ❤️ I]
C# Unicode normalization
The same grapheme can be encoded by different sequences of code points. To
compare such strings, they must be brought to a common form with
Normalize. Normalization form C (NFC) prefers precomposed
characters, form D (NFD) decomposes them into base characters and combining
marks.
using System.Globalization;
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
string composed = "caf\u00E9";
string decomposed = "cafe\u0301";
Console.WriteLine($"{composed} and {decomposed}");
Console.WriteLine($"Lengths: {composed.Length} and {decomposed.Length}");
Console.WriteLine($"Ordinal equality: {composed == decomposed}");
Console.WriteLine($"Culture comparison: "
+ string.Compare(composed, decomposed, CultureInfo.InvariantCulture, CompareOptions.None));
string nfc = decomposed.Normalize(NormalizationForm.FormC);
string nfd = composed.Normalize(NormalizationForm.FormD);
Console.WriteLine($"After NFC: {nfc == composed}, length {nfc.Length}");
Console.WriteLine($"After NFD: {nfd == decomposed}, length {nfd.Length}");
Console.WriteLine($"Is normalized NFC: {composed.IsNormalized()}, {decomposed.IsNormalized()}");
We compare the two forms of the word café.
Console.WriteLine($"Ordinal equality: {composed == decomposed}");
The == operator compares code units, so the strings differ.
Console.WriteLine($"Culture comparison: "
+ string.Compare(composed, decomposed, CultureInfo.InvariantCulture, CompareOptions.None));
A culture-aware comparison treats canonically equivalent strings as equal and returns 0.
string nfc = decomposed.Normalize(NormalizationForm.FormC); string nfd = composed.Normalize(NormalizationForm.FormD);
After normalization to the same form, the strings are equal also in an
ordinal comparison. Normalizing input text to NFC before storing or
comparing it is a common practice. IsNormalized checks whether
a string is already in form C.
$ dotnet run café and café Lengths: 4 and 5 Ordinal equality: False Culture comparison: 0 After NFC: True, length 4 After NFD: True, length 5 Is normalized NFC: True, False
Source
In this article we have worked with graphemes, code points, surrogate pairs, and normalization of Unicode strings in C#. See also the string, Encoding, and char tutorials.
Author
List all C# tutorials.