Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if character has case in JAVA

In JAVA (Android), I'm trying to determine in a String, if each character has a equivalent in upper or lower case.

My goal is not to lower or upper the case, but to know if it's possible.

For example this function would return true for : 'e' 'é' 'i' 'l' 'L' 'O' 'P' and false for emojis or chinese characters.

Is there any function that can do this?

EDIT : To be more clear, the function was supposed to take a character for argument, not a String and return false if the character had no uppercase or lowercase version.

like image 504
liltof Avatar asked Jul 29 '26 21:07

liltof


2 Answers

You can try this:

boolean validate(char c){
    return Character.isUpperCase(c) || Character.isLowerCase(c);
}

This will return true iff it is a Letter in uppercase or lower case only. Otherwise it'll return false.

like image 51
Kaushal28 Avatar answered Aug 01 '26 10:08

Kaushal28


For a simple method, there's Character.isLowerCase. But you actually need to be careful- it depends on language. Some languages may have a lower case 'é' but no uppercase. Or like the turkish "I" may have a different lower case version than other languages.

To work around that, I'd use something like Character.isLetter(myChar) && String.valueOf(myChar).toLowerCase().equals(String.valueOf(myChar)). Remember to use the version of toLowerCase that takes a Locale as parameter if not comparing in the default Locale.

like image 24
Gabe Sechan Avatar answered Aug 01 '26 09:08

Gabe Sechan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!