While we are processing Characters, it’s a very common practice that we use ASCII values…
I still remember in my computer class if I had to find out a character is a capital letter or not I would compare it with its ASCII value
I have also seen these comparisons
if (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’) – This means our character is an ALPHABET
if (ch == ‘ ’) – This means our character is a whitespace
Now this comparison is language specific…I mean it works for English and some other international languages but fails for others…
If you are interested in having an internationally accepted Character Test Code ready then read on
Char cTest;
If (Character.isLetter(cTest) – This mean cTest is an Alphabet
If (Character.isDigit(cTest) – This mean cTest is a Digit
If (Character.isSpaceChar(cTest) – This mean cTest is a Space
There is a very useful function called getType which can return meaningful constants which will tell you the type of the character
if (Character.getType('s') == Character.LOWERCASE_LETTER) - Lower Case Letter
if (Character.getType('_') == Character.CONNECTOR_PUNCTUATION)
Enjoy!!!!
Comments