---
title: "Normalize JavaScript Unicode text in C#"
url: https://recursiveai.org/oneecma/cookbook/normalize-javascript-unicode-text/
description: "Run JavaScript String.normalize in C# with .NET invariant globalization enabled. Compare NFC and NFKC on a combining accent and a ligature."
version: "1.1.1"
---

# Normalize JavaScript Unicode text in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/normalize-javascript-unicode-text/normalize-javascript-unicode-text.zip)

OneEcma normalizes text with `InvariantGlobalization` enabled. It ships its own normalization data and doesn’t call .NET’s `string.Normalize`.

```shell
dotnet add package RecursiveAI.OneEcma
```

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

```csharp
using RecursiveAI.OneEcma;
Console.WriteLine(new ScriptEngine()
  .Evaluate("""
const text = 'e\u0301 \uFB01';
text.normalize('NFC') + '\n'
  + text.normalize('NFKC');
""").AsString);
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/normalize-javascript-unicode-text/normalize-javascript-unicode-text.zip), then `dotnet run`.

Prints:

```text
é ﬁ
é fi
```

## The full program

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const text = 'e\u0301 \uFB01';
function codePoints(value) {
  return Array.from(value, ch => 'U+' + ch.codePointAt(0)
    .toString(16).toUpperCase().padStart(4, '0')).join(' ');
}
const nfc = text.normalize('NFC');
const nfkc = text.normalize('NFKC');
[
  `Input: ${codePoints(text)}`,
  `NFC: ${nfc} (${codePoints(nfc)})`,
  `NFKC: ${nfkc} (${codePoints(nfkc)})`
].join('\n');
""").AsString);
```

`dotnet run` prints:

```text
Input: U+0065 U+0301 U+0020 U+FB01
NFC: é ﬁ (U+00E9 U+0020 U+FB01)
NFKC: é fi (U+00E9 U+0020 U+0066 U+0069)
```

The download enables invariant globalization in `Example.csproj`. To run the snippet at the top in the same .NET mode, add this block inside your project file’s `<Project>` element:

```xml
<PropertyGroup>
  <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
```

A form name other than `NFC`, `NFD`, `NFKC`, or `NFKD` raises a JavaScript `RangeError`.

## Variations

Use `NFD` to get the decomposed code points. Replace the JavaScript inside `Evaluate` with the code below. It prints `65 301`.

```javascript
const text = '\u00E9';
Array.from(text.normalize('NFD'), ch =>
  ch.codePointAt(0).toString(16)
).join(' ');
```

## Related recipes

- [Sort JavaScript strings by locale in C#](https://recursiveai.org/oneecma/cookbook/sort-javascript-strings-with-locale-collation/)
- [Split JavaScript text into graphemes in C#](https://recursiveai.org/oneecma/cookbook/segment-javascript-text-into-graphemes/)
- [Format JavaScript dates by locale and time zone in C#](https://recursiveai.org/oneecma/cookbook/format-javascript-dates-by-locale/)

## The short answer

Call JavaScript `String.prototype.normalize` through OneEcma’s `ScriptEngine`. `NFC` composes `e` and its combining acute accent into `é`; `NFKC` also replaces the single `ﬁ` ligature with `f` and `i`. The downloadable project enables .NET invariant globalization and prints the Unicode code points as well as the text, so you can see which characters changed.

## What’s the difference between NFC and NFKC in JavaScript?

NFC gives canonically equivalent text the same representation. An `é` typed as U+00E9 and an `e` followed by U+0301 are canonically equivalent; both become U+00E9 in this example. NFC leaves the ligature U+FB01 alone. NFKC also applies compatibility mappings before composition. Here, that means rewriting U+FB01 as U+0066 and U+0069. The [Unicode normalization specification](https://www.unicode.org/reports/tr15/) explains both types of equivalence. [`String.prototype.normalize()` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) describes the four names and the `NFC` default when you omit the argument.

Use NFC if you’re comparing strings that came from different keyboards or input methods and you want canonical equivalence without compatibility folding. NFKC can be useful when your search should match `ﬁle` to `file`. Keep the original string separately if it must be displayed as entered. Don’t apply NFKC blindly to identifiers whose original distinctions matter: the process loses information, and you can’t recover the original ligature from the resulting `fi`. Neither form in this example removes the acute accent.

## Does JavaScript normalization work with `InvariantGlobalization` enabled?

In OneEcma, yes. This recipe calls the JavaScript method, not .NET’s `string.Normalize`. The pinned [text services docs](https://recursiveai.org/oneecma/docs/text-services/) say, “Nothing here uses the platform's globalization or normalization support.” The engine carries Unicode 17.0.0 normalization data. Enabling `.NET`’s [invariant globalization mode](https://learn.microsoft.com/en-us/dotnet/core/runtime-config/globalization) in the example affects the .NET process, not the data OneEcma uses for this JavaScript operation. This doesn’t imply that unrelated .NET culture APIs get locale data from OneEcma.

## When would you use NFD instead of NFC?

NFD decomposes canonically equivalent text but doesn’t compose it again. Use it when you need to inspect a base character and its combining marks separately. In the variation, a single `é` becomes U+0065 followed by U+0301. NFD alone doesn’t strip that mark, and decomposed text can have a different UTF-16 length from composed text. If you normalize before processing ranges, compute the offsets from the normalized string, not from the original.
