In Unicode, some characters can be written in more than one way. The letter é can be one code point (U+00E9, “precomposed”) or two: a plain e followed by a combining accent (U+0301, “decomposed”). They look identical on screen, but == says they are different, because the chars differ.
Normalize() rewrites a string into one standard form, so both spellings end up the same. With no argument it uses Form C (combine where possible), which is the common choice for storing and comparing text. You can also ask for Form D (split apart) or the compatibility forms KC and KD. A new string is returned.
This is an instance member of System.String. Call it on a string variable, such as text.Normalize(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Normalize() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.
Syntax
Call it with no argument for Form C, or pass a NormalizationForm (in System.Text).
C#
using System.Text;
string decomposed = "e\u0301"; // e + combining acute accent
string composed = decomposed.Normalize(); // "\u00e9" (one char)
string split = composed.Normalize(NormalizationForm.FormD); // back to two chars
Call
What it does
text.Normalize()
Form C: combine letters and accents into single code points.
text.Normalize(NormalizationForm.FormD)
Form D: split letters and accents apart.
text.Normalize(NormalizationForm.FormKC)
Compatibility + combine: also turns look-alikes such as ligatures and full-width letters into plain ones.
text.Normalize(NormalizationForm.FormKD)
Compatibility + split.
Normalize both sides the same way before comparing. Plain ASCII text is already normalized in every form, so it comes back unchanged. The K forms change meaning slightly (for example, a superscript 2 becomes a normal 2), so use them for search and matching, not for storing what the user wrote. Text with invalid Unicode throws ArgumentException.
Library declarations
C#
public string Normalize();
public string Normalize(NormalizationForm normalizationForm);
// System.Text
public enum NormalizationForm { FormC = 1, FormD = 2, FormKC = 5, FormKD = 6 }
Parameters
Parameter
Description
normalizationForm
Optional. Which standard form to produce. The default is FormC.
Return Value
Return Value
Description
string
A string in the requested form. If the text was already in that form, the same text comes back.
C# Examples
Example 1 — Make Two Spellings of é Equal
Compare a precomposed and a decomposed é, then normalize and compare again.
C#
using System;
class Program {
static void Main() {
string composed = "\u00e9";
string decomposed = "e\u0301";
Console.WriteLine(composed == decomposed);
Console.WriteLine(composed.Length);
Console.WriteLine(decomposed.Length);
Console.WriteLine(composed == decomposed.Normalize());
}
}
Output
False
1
2
True
How It Works
Same look, different chars. Both strings display as é, but one has a single code point and the other has two.
== compares chars. Since the char sequences differ, the first test is False.
Lengths reveal it. The precomposed form has length 1; the decomposed form has length 2.
Form C combines.Normalize() joins e and the accent into U+00E9, and now the strings are equal.
Example 2 — Remove Accents for a Search Key
Split letters from their accents with Form D, then drop the accent marks to build a plain search key.
C#
using System;
using System.Globalization;
using System.Text;
class Program {
static void Main() {
string word = "caf\u00e9";
string split = word.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
foreach (char c in split) {
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) {
sb.Append(c);
}
}
Console.WriteLine(word.Length);
Console.WriteLine(split.Length);
Console.WriteLine(sb.ToString());
}
}
Output
4
5
cafe
How It Works
Split apart. Form D turns é into e plus a combining accent, so the length grows from 4 to 5.
Find the marks.CharUnicodeInfo.GetUnicodeCategory reports NonSpacingMark for combining accents.
Keep the rest. Every other character is appended, which leaves the plain letters cafe.
For matching only. Use a key like this to search; keep the original word with its accent for display.
Example 3 — Turn Ligatures and Full-Width Letters into Plain Text
A string with the fi ligature and a full-width A becomes plain ASCII with Form KC.
C#
using System;
using System.Text;
class Program {
static void Main() {
string s = "\uFB01le \uFF21";
string plain = s.Normalize(NormalizationForm.FormKC);
Console.WriteLine(plain);
Console.WriteLine(s.Length);
Console.WriteLine(plain.Length);
Console.WriteLine(s.Normalize() == s);
}
}
Output
file A
5
6
True
How It Works
Look-alike characters. U+FB01 is a single “fi” ligature and U+FF21 is a full-width capital A, both common in copied PDF or Asian text.
KC simplifies them. The compatibility form replaces the ligature with f + i and the full-width letter with a normal A.
One char becomes two. The ligature expands, so the length goes from 5 to 6.
Form C leaves them alone. These are compatibility differences only, so plain Normalize() returns equal text and the last line is True.
Frequently Asked Questions
It returns the string in a standard Unicode form, so characters that can be written in several ways (like é) are written one consistent way.
NormalizationForm.FormC, which combines letters and accents into single code points where possible.
Normalize to FormD, then skip characters whose Unicode category is NonSpacingMark. Example 2 on this page shows the full code.
FormC only joins canonical equivalents such as e + accent. FormKC also replaces compatibility characters, such as ligatures and full-width letters, with their plain versions.
When text comes from different sources (keyboards, files, web pages), normalizing both sides to the same form makes equal-looking text compare equal.