Nicolai Nicolai - 1 year ago 123
Java 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

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)
isLower(String s)
, to check if all the characters in the String are upper or lower case?


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.


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
is the best way to go.

Answer Source

Guava's CharMatchers tend to offer very expressive and efficient solutions to this kind of problem.

CharMatcher.JAVA_UPPER_CASE.matchesAllOf("AAA"); // true
CharMatcher.JAVA_UPPER_CASE.matchesAllOf("A SENTENCE"); // false
CharMatcher.JAVA_UPPER_CASE.or(CharMatcher.WHITESPACE).matchesAllOf("A SENTENCE"); // true
CharMatcher.JAVA_UPPER_CASE.or(CharMatcher.JAVA_LETTER.negate()).matchesAllOf("A SENTENCE"); // true
CharMatcher.JAVA_LOWER_CASE.matchesNoneOf("A SENTENCE"); // true

A static import for* can help make these more succinct.

JAVA_LOWER_CASE.matchesNoneOf("A SENTENCE"); // true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download