To use
isDigit
in Java, call it like so: Character.isDigit('1')
.With a character variable
If you have a single char
variable, then you can determine whether the char
is a digit like so:
char digit = '1';
char notDigit = 'c';
System.out.println(Character.isDigit(digit)); // true
System.out.println(Character.isDigit(notDigit)); // false
With a string variable
If you want to know whether a single char
in a String
is a digit, first extract the char
at index, then call isDigit
:
String string = "Hello World 42!";
System.out.println(Character.isDigit(string.charAt(1))); // e -> false
System.out.println(Character.isDigit(string.charAt(12))); // 4 -> true