Character Classification IsDigit IsLetter
Classify each char with isLetter, isDigit, and isWhitespace to build a whitelist sanitizer, and use the codePoint variants for astral plane input.
Code
public class FieldSanitizer {
public static void main(String[] args) {
String raw = "id: 42, note: café №5";
// 白名单 sanitizer:保留字母数字与空格,其余替换
for (char c : raw.toCharArray()) {
if (Character.isLetterOrDigit(c) || c == ':' || c == ' ') {
System.out.print(c);
} else if (Character.isWhitespace(c)) {
System.out.print('_');
} else {
System.out.print('?');
}
}
System.out.println();
// 分类 API 各自独立:letter、digit、大小写转换
for (char c : "a9é".toCharArray()) {
System.out.printf("%c letter=%b digit=%b upper=%s%n",
c,
Character.isLetter(c),
Character.isDigit(c),
Character.toUpperCase(c));
}
// 增补平面字符(emoji)用 int 版本重载:char 版本只看一个 UTF-16 单元
int rocket = "🚀".codePointAt(0);
System.out.println("rocket isLetter=" + Character.isLetter(rocket));
}
}How it works
Character Classification IsDigit IsLetter comes down to the class FieldSanitizer and the function main.
Repetition is confined to for (char c : raw.toCharArray()) — the rest of the file is straight-line.
Applying it
The demo output is labeled (rocket isLetter=), so every line of output can be traced to the operation that made it.
Statements that matter
String raw = "id: 42, note: café №5";
int rocket = "🚀".codePointAt(0);
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.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.
FAQ
- What does Character Classification IsDigit IsLetter print when run?
- Execute the whole file; each printed line is prefixed with a label like `rocket isLetter=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.