|
|
|
|||
|
Checking Character PropertiesYou can categorize characters according to their properties. For instance, X is an uppercase letter and 4 is a decimal digit. Checking character properties is a common way to verify the data entered by end users. If you are selling books online, for example, your order entry screen should verify that the characters in the quantity field are all digits.Developers who aren't used to writing global software might determine a character's properties by comparing it with character constants. For instance, they might write code like this: char ch; ... // This code is WRONG! if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) // ch is a letter ... if (ch >= '0' && ch <= '9') // ch is a digit ... if ((ch == ' ') || (ch =='\n') || (ch == '\t')) // ch is a whitespace The preceding code is wrong because it works only with English and a few other languages. To internationalize the previous example, replace it with the following statements: Thechar ch; ... // This code is OK! if (Character.isLetter(ch)) ... if (Character.isDigit(ch)) ... if (Character.isSpaceChar(ch)) Characterchar values represent Unicode characters. If you check
the properties of a char with the appropriate
Character method, your code will work with all major
languages. For example, the Character.isLetter method
returns true if the character is a letter in Chinese,
German, Arabic, or another language.
The following list gives some of the most useful
The
| |||
|