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.
Code
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
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.Regular Expression TesterTest regular expressions against sample text with live match highlighting, capture group extraction, and flags support for global, case-insensitive, and multiline modes.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.