Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Character.isLetter(int) — test whether a code point is a letter

Check whether one Unicode code point is a letter, even when its UTF-16 representation takes two `char` code units.

Version: Java SE 21

Syntax

public static boolean isLetter(int codePoint)

boolean letter = Character.isLetter(codePoint);

Character.isLetter(int) is a static method in java.lang.Character. It takes one Unicode code point as an int and returns true only for the uppercase (Lu), lowercase (Ll), titlecase (Lt), modifier (Lm), or other letter (Lo) categories. Java SE 21 uses Unicode 15.0 for this classification, regardless of locale.

The Character.isLetter(char) overload tests one 16-bit UTF-16 code unit. A code point above U+FFFF takes two such units, called a surrogate pair. When processing a String, use codePoints() to combine each valid pair into one int; charAt(int) returns only one unit.

The method does not test a whole string or a character as a reader perceives it, which may consist of several code points. For example, é (U+00E9) is a letter, but the combining acute accent (U+0301) that can follow e is not a letter on its own.

Examples

public class Main {
    public static void main(String[] args) {
        String text = "A7\u0301\uD840\uDC00";
        text.codePoints().forEachOrdered(cp ->
            System.out.println(Character.isLetter(cp))
        );
        System.out.println("charAt(3): " + Character.isLetter(text.charAt(3)));
    }
}

Result

true
false
false
true
charAt(3): false

The first four results are for `A`, the digit `7`, the combining acute accent U+0301, and the ideograph U+20000, in that order. The last result is `false` because `charAt(3)` gets only the first surrogate of the ideograph and the `char` overload is called.

Related lessons

  • Java · Lesson 63 · Basic · Version: 2

Sources