50

What is the difference between Character.isAlphabetic() and Character.isLetter() in Java? When should one use one and when should one use the other?

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59

1 Answers1

47

According to the API docs, isLetter() returns true if the character has any of the following general category types: UPPERCASE_LETTER (Lu), LOWERCASE_LETTER (Ll), TITLECASE_LETTER (Lt), MODIFIER_LETTER (Lm), OTHER_LETTER (Lo). If we compare isAlphabetic(), it has the same but adds LETTER_NUMBER (Nl), and also any characters having Other_Alphabetic property.

What does this mean in practice? Every letter is alphabetic, but not every alphabetic is a letter - in Java 7 (which uses Unicode 6.0.0), there are 824 characters in the BMP which are alphabetic but not letters. Some examples include 0345 (a combiner used in polytonic Greek), Hebrew vowel points (niqqud) starting at 05B0, Arabic honorifics such as saw ("peace be upon him") at 0610, Arabic vowel points... the list goes on.

But basically, for English text, the distinction makes no difference. For some other languages, the distinction might make a difference, but it is hard to predict in advance what the difference might be in practice. If one has a choice, the best answer may be isLetter() - one can always change to permit additional characters in the future, but reducing the set of accepted characters might be harder.

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
  • Does "." and "," and other symbols used in sentences ("?", "!", ...) belong to isAlphabetic? – android developer Oct 22 '15 at 11:54
  • 3
    @androiddeveloper: No, they don't. For example, the period / full stop (U+002E) has general category OTHER_PUNCTUATION (Po), which is not one of the listed general categories for isLetter() / isAlphabetic(), nor does it have the Other_Alphabetic property. The same is true for the other punctuation marks you mentioned. – Simon Kissane Oct 22 '15 at 13:30
  • OK, thank you. So the isAlphabetic is only contains letters and signs that are inside of letter, but not for characters that are used for sentences. Is this an easier and better way to understand it? – android developer Oct 22 '15 at 14:00
  • 2
    @androiddeveloper, I take it by "characters that are used for sentences" you mean punctuation marks? Yes isAlphabetic() and isLetter() both exclude those. – Simon Kissane Oct 22 '15 at 21:08