Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Character.isDigit()

Checks whether a character is a digit. Related checks are isLetter(), isWhitespace(), isUpperCase().

Available since: Java 1.0

Syntax

static boolean isDigit(char ch)

Parameters

ParameterTypeDescription
chcharThe character to check.

Return value

booleantrue if the character is a digit.

Examples

Classifying characters:

void main() {
    for (char c : "a1 B".toCharArray()) {
        IO.println("'" + c + "' digit=" + Character.isDigit(c)
                + " letter=" + Character.isLetter(c)
                + " upper=" + Character.isUpperCase(c)
                + " space=" + Character.isWhitespace(c));
    }
}

Output:

'a' digit=false letter=true upper=false space=false
'1' digit=true letter=false upper=false space=false
' ' digit=false letter=false upper=false space=true
'B' digit=false letter=true upper=true space=false

A digit character to a number, and changing case:

void main() {
    char c = '7';
    IO.println(Character.getNumericValue(c) + 1);
    IO.println(c - '0');
    IO.println(Character.toUpperCase('q'));
}

Output:

8
7
Q

Notes

isDigit() and isLetter() understand all of Unicode: Cyrillic letters are letters, and Arabic-Indic digits are digits. If you need only 0–9, check the range: c >= '0' && c <= '9'.

See also

Related recipes

In the tutorial

  • String methods — The main String methods in Java: searching, substring, changing case, strip, replace, split, join, repeat and string checks.

Thanks for reading!

Donate to the site — we work hard for you.