Java Character isDigit() Method
Example
Count the digits in a string:
public class Main {
public static void main(String[] args) {
String text = "Volvo 240";
int count = 0;
for (char c : text.toCharArray()) {
if (Character.isDigit(c)) {
count++;
}
}
System.out.println("Digits: " + count);
}
}
Definition and Usage
The isDigit() method checks if a character is a digit. It returns true for the characters '0' to '9', and false for letters, spaces and symbols.
It also returns true for digits from other writing systems, like Arabic-Indic digits.
Tip: Use getNumericValue() to get the number that a digit stands for.
Syntax
One of the following:
public static boolean isDigit(char ch)
public static boolean isDigit(int codePoint)
Most programs use the first version, with a char. The second version takes the character as a code point (the number that Unicode gives each character), as an int.
Parameter Values
| Parameter | Description |
|---|---|
| ch | Required. The character to check. |
| codePoint | Required. The code point of the character to check. |
Technical Details
| Returns: | true if the character is a digit, otherwise false. |
|---|
Related Pages
❮ Character Methods