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.

JavaForm Validation

Code

snippet
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

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=`.