Java Internationalization: Character Methods
Jakob Jenkov |
Java has some methods in the java.lang.Character class that are useful in
internationalization of Java applications. These methods can tell if a given character
is a letter, number, space etc. across different languages. The methods work on unicode
characters.
When dealing with letters and symbols from many different languages it is not safe
to just check if a character (ascii value) is in range of the lowercase or upppercase letters,
or within the number range of ascii values as was normal when only ascii characters were used.
To safely check if a character is a letter, number or space character you need to use
the methods in java.lang.Character to do so.
The methods are:
Character.isDigit() Character.isLetter() Character.isLetterOrDigit() Character.isLowerCase() Character.isUpperCase() Character.isSpaceChar()
Each of these methods take a char as parameter. Here are a few usage examples:
char aChar = 'C'; Character.isDigit (aChar); // returns false Character.isLetter(aChar); // returns true Character.isLetterOrDigit(aChar); // returns true Character.isLowerCase(aChar); // returns false Character.isUpperCase(aChar); // returns true Character.isSpaceChar(aChar); // returns false
getType()
The Character class also has a getType() method which returns
the type of a certain character. The returned type is an int which matches
a set of predefined constants in the Character class. Here is an example:
char aChar = 'æ';
int type = Character.getType(aChar);
if(type == Character.LOWERCASE_LETTER) { ... }
if(type == Character.UPPERCASE_LETTER) { ... }
There are several other constants you can check the character type against.
Look in the JavaDoc for the Character class for more constants.
isDefined()
The Character class also has a method that enables you to determine if
the value of a given int defines a character in unicode. The method
is called isDefined(). Here is an example:
char aChar = 'A'; int anInt = aChar; bolean isDefined = Character.isDefined(anInt);
| Tweet | |
Jakob Jenkov | |











