Unicode Normalizer NFC NFD Matching

Normalize Unicode text with Normalizer before comparing: composed e-acute and e plus combining accent are byte-identical in NFD but differ in raw length.

JavaData Integrity

Code

snippet
import java.text.Normalizer;

public class UnicodeFormMatch {

    public static void main(String[] args) {
        // 同一个「é」的两种字节形态:预组装 U+00E9 与 e + 组合符 U+0301
        String composed = "café";
        String decomposed = "cafe\u0301";

        System.out.println("lengths: " + composed.length() + " vs " + decomposed.length());

        // matching 前先归一到同一形态:NFD 下两者完全一致
        String a = Normalizer.normalize(composed, Normalizer.Form.NFD);
        String b = Normalizer.normalize(decomposed, Normalizer.Form.NFD);
        System.out.println("equal after NFD: " + a.equals(b));

        System.out.println("composed is NFC: "
                + Normalizer.isNormalized(composed, Normalizer.Form.NFC));
        System.out.println("decomposed is NFC: "
                + Normalizer.isNormalized(decomposed, Normalizer.Form.NFC));
    }
}

How it works

Count the class UnicodeFormMatch and the function main as the entire surface area of Unicode Normalizer NFC NFD Matching.

Putting it to use

Running the file prints labeled results such as lengths:, equal after NFD:, composed is NFC: — each operation’s effect is visible in the output.

Core lines

String b = Normalizer.normalize(decomposed, Normalizer.Form.NFD);
String a = Normalizer.normalize(composed, Normalizer.Form.NFD);
String decomposed = "cafe\u0301";

Interactive Tools for This Topic

FAQ

What does Unicode Normalizer NFC NFD Matching print when run?
No setup needed: run the file and look for labeled lines like `lengths:`, `equal after NFD:`, `composed is NFC:` in the output.