NFKC Folds Fullwidth And Superscript Forms
Fold fullwidth Latin, superscript digits, and ligatures to base letters with NFKC before validating input, so length checks see real characters.
Code
import java.text.Normalizer;
public class UserInputNormalizer {
// NFKC folds fullwidth and superscript forms to base letters, 校验前的关键一步
static String fold(String raw) {
return Normalizer.normalize(raw, Normalizer.Form.NFKC);
}
public static void main(String[] args) {
String fullwidth = "TOKYO"; // fullwidth Latin
String superscript = "x² + y³"; // superscript digits
String ligature = "file"; // fi ligature
System.out.println(fold(fullwidth));
System.out.println(fold(superscript));
System.out.println(fold(ligature));
// 长度变化:兼容字符折叠后每个字符都还原成基础字母
System.out.println("file".length() + " -> " + fold("file").length());
}
}What the snippet does
The working surface is the class UserInputNormalizer and the functions fold and main — everything else is wiring.
Caveats
- No outside surface: every behavior is standard library.
Putting it to use
Signature: static String fold(String raw)
Output lines are prefixed (file), so each line of output can be traced back to its step.
main calls 1 helper in sequence: fold.
Core lines
return Normalizer.normalize(raw, Normalizer.Form.NFKC);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.
FAQ
- What does NFKC Folds Fullwidth And Superscript Forms print when run?
- Running the file directly is enough — expect labeled output lines such as `file`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.