Question

Is there an existing library method that checks if a String is all upper case or lower case in Java?

I know there are plenty of upper() methods in Java and other frameworks like Apache commons lang, which convert a String to all upper case.

Are there any common libraries that provide a method like isUpper(String s) and isLower(String s), to check if all the characters in the String are upper or lower case?

EDIT:

Many good answers about converting to Upper and comparing to this. I guess I should have been a bit more specific, and said that I already had thought of that, but I was hoping to be able to use an existing method for this.

Good comment about possible inclusion of this in apache.commons.lang.StringUtils. Someone has even submitted a patch (20090310). Hopefully we will see this soon. https://issues.apache.org/jira/browse/LANG-471

EDIT:

What I needed this method for, was to capitalize names of hotels that sometimes came in all uppercase. I only wanted to capitalize them if they were all lower or upper case. I did run in to the problems with non letter chars mentioned in some of the posts, and ended up doing something like this:

private static boolean isAllUpper(String s) {
    for(char c : s.toCharArray()) {
       if(Character.isLetter(c) && Character.isLowerCase(c)) {
           return false;
        }
    }
    return true;
}

This discussion and differing solutions (with different problems), clearly shows that there is a need for a good solid isAllUpper(String s) method in commons.lang

Until then I guess that the myString.toUpperCase().equals(myString) is the best way to go.

 45  101366  45
1 Jan 1970

Solution

 26

This if condition can get the expected result:

String input = "ANYINPUT";

if(input.equals(input.toUpperCase())
{
   // input is all upper case
}
else if (input.equals(input.toLowerCase())
{
   // input is all lower case
}
else
{
   // input is mixed case
}
2013-09-24