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
| Parameter | Type | Description |
|---|---|---|
| ch | char | The 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=falseA 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
QNotes
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
- String.toCharArray() — Copies the characters of a string into a new char[] array.
- String.chars() — Returns a stream of the character codes of a string.
- Integer.parseInt() — Converts a string containing an integer to an int.
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.