The Character class is a subclass of the Object class, which wraps the value of the original type char in an object. An object of type Character contains a single field of type char. We can use isDigit() method of Character class to check if a given character in a string is a number/letter. The isDigit() method is a static method used to determine whether the specified character is a digit.
public class CharacterIsNumberOrDigitTest { public static void main(String[] args) { String str = "Tutorials123"; for(int i=0; i < str.length(); i++) { <strong>Boolean </strong>flag = <strong>Character.isDigit(str.charAt(i))</strong>; if(flag) { System.out.println("'"+ str.charAt(i)+"' is a number"); } else { System.out.println("'"+ str.charAt(i)+"' is a letter"); } } } }
'T' is a letter 'u' is a letter 't' is a letter 'o' is a letter 'r' is a letter 'i' is a letter 'a' is a letter 'l' is a letter 's' is a letter '1' is a number '2' is a number '3' is a number
The above is the detailed content of How to check if a given character is a number or letter in Java?. For more information, please follow other related articles on the PHP Chinese website!