Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for English only A-Z upper case of a character

Tags:

java

uppercase

I need to test a character for uppercase only A-Z. Not any other special unicode or other languages.

I was reading the documentation for Character.isUpperCase. It seems like it would pass if it was a unicode character that was considered uppercase but not technically between A-Z. And it seems like it would pass uppercase characters from other languages besides english.

Do i just need to use regular expressions or am i reading into Character.isUpperCase incorrectly?

Thanks

like image 791
prolink007 Avatar asked Dec 25 '22 02:12

prolink007


2 Answers

From the documentation you linked:

Many other Unicode characters are uppercase too.

So yes, using isUpperCase will match things other than A-Z. One way to do the test though is like this.

boolean isUpperCaseEnglish(char c){
    return c >= 'A' && c <= 'Z';
}
like image 129
resueman Avatar answered Dec 28 '22 09:12

resueman


isUpperCase indeed does not promise the character is between 'A' and 'Z'. You could use a regex:

String s = ...;
Pattern p = Pattern.compile("[A-Z]*");
Matcher m = p.matcher(s);
boolean matches = m.matches();
like image 37
Mureinik Avatar answered Dec 28 '22 11:12

Mureinik