Check the character type

ReturnMethodSummary
static booleanisDefined(char ch)if a character is defined in Unicode.
static booleanisDigit(char ch)if the character is a digit.
static booleanisHighSurrogate(char ch)if the char is a high-surrogate code unit.
static booleanisIdentifierIgnorable(char ch)if the character should be regarded as an ignorable character in a Java identifier or a Unicode identifier.
static booleanisISOControl(char ch)if the character is an ISO control character.
static booleanisJavaIdentifierPart(char ch)if the character may be part of a Java identifier as other than the first character.
static booleanisJavaIdentifierStart(char ch)if the character is permissible as the first character in a Java identifier.
static booleanisLetter(char ch)if the character is a letter.
static booleanisLetterOrDigit(char ch)if the character is a letter or digit.
static booleanisLowerCase(char ch)if the character is a lowercase character.
static booleanisLowSurrogate(char ch)if the char value is a low-surrogate code unit.
static booleanisMirrored(char ch)whether the character is mirrored according to the Unicode specification.
static booleanisSpaceChar(char ch)if the character is a Unicode space character.
static booleanisSurrogatePair(char high, char low)whether the pair of char values is a valid surrogate pair.
static booleanisTitleCase(char ch)if the character is a titlecase character.
static booleanisUnicodeIdentifierPart(char ch)if the character may be part of a Unicode identifier as other than the first character.
static booleanisUnicodeIdentifierStart(char ch)if the character is permissible as the first character in a Unicode identifier.
static booleanisUpperCase(char ch)if the character is an uppercase character.
static booleanisWhitespace(char ch)if the character is white space according to Java.

public class Main{
  public static void main(String[] argv){
    System.out.println("is digit:"+Character.isDigit('a'));    
    System.out.println("is letter:"+Character.isLetter('a'));    
    System.out.println("is lowercase:"+Character.isLowerCase('a'));    
    System.out.println("is space char:"+Character.isSpaceChar('a'));    
    System.out.println("is white space:"+Character.isWhitespace('a'));    
    System.out.println("is upper case:"+Character.isUpperCase('a'));    
    System.out.println("is Java identifier start:"+Character.isJavaIdentifierStart('a'));    

  }
}

The output:


is digit:false
is letter:true
is lowercase:true
is space char:false
is white space:false
is upper case:false
is Java identifier start:true

The following code uses a for loop to check the each value in a char array.


  public class Main {
  public static void main(String args[]) {
    char a[] = {'J', 'a', 'v', 'A', '2', '.','c','O','M'};

    for (int i = 0; i < a.length; i++) {
      if (Character.isDigit(a[i]))
        System.out.println(a[i] + " is a digit.");
      if (Character.isLetter(a[i]))
        System.out.println(a[i] + " is a letter.");
      if (Character.isWhitespace(a[i]))
        System.out.println(a[i] + " is whitespace.");
      if (Character.isUpperCase(a[i]))
        System.out.println(a[i] + " is uppercase.");
      if (Character.isLowerCase(a[i]))
        System.out.println(a[i] + " is lowercase.");
    }
  }
}

The output:


J is a letter.
J is uppercase.
a is a letter.
a is lowercase.
v is a letter.
v is lowercase.
A is a letter.
A is uppercase.
2 is a digit.
c is a letter.
c is lowercase.
O is a letter.
O is uppercase.
M is a letter.
M is uppercase.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.