Question

How can I determine if String contains only alphabets and I want to have little more than [a-zA-Z]+, so is there any way to determine alphabets by Locale?

Was it helpful?

Solution

The Character class has methods such as isLetter which will be able to determine if a character is a letter as defined in the API specification for the method.

There is also another approach of using the Character.UnicodeBlock class which is able to return whether a character is in a specific character block of Unicode.

For example, I had to determine whether a character was a full-width katakana character, and to be able to do so, I had to use the Character.UnicodeBlock.of method:

boolean isKatakana = 
    Character.UnicodeBlock.of(c) == Character.UnicodeBlock.KATAKANA;

Also to add, the character at a certain index of a String can be retrieved by using the charAt(int) method, and the Unicode code point can be retrieved by the codePointAt(int) method.

OTHER TIPS

In addition take a look at com.ibm.icu.text package. For example, this snippet returns all alphabet letters for defined language:

ULocale ulocale = com.ibm.icu.util.ULocale
    .forLocale(Locale.forLanguageTag(language));
UnicodeSet set = LocaleData.getExemplarSet(ulocale, LocaleData.ES_STANDARD);
Iterator<String> iterator = set.iterator();
StringBuffer buf = new StringBuffer();
while (iterator.hasNext()) {
  buf.append(iterator.next());

}
return buf.toString();

This task cannot be reached by the means of java.* packages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top