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

Viewed 90111

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.

11 Answers

Not a library function unfortunately, but it's fairly easy to roll your own. If efficiency is a concern, this might be faster than s.toUpperCase().equals(s) because it can bail out early.

public static boolean isUpperCase(String s)
{
    for (int i=0; i<s.length(); i++)
    {
        if (!Character.isUpperCase(s.charAt(i)))
        {
            return false;
        }
    }
    return true;
}

Edit: As other posters and commenters have noted, we need to consider the behaviour when the string contains non-letter characters: should isUpperCase("HELLO1") return true or false? The function above will return false because '1' is not an upper case character, but this is possibly not the behaviour you want. An alternative definition which would return true in this case would be:

public static boolean isUpperCase2(String s)
{
    for (int i=0; i<s.length(); i++)
    {
        if (Character.isLowerCase(s.charAt(i)))
        {
            return false;
        }
    }
    return true;
}

Not that i know.

You can copy the string and convert the copy to lower/upper case and compare to the original one.

Or create a loop which checks the single characters if the are lower or upper case.

This method might be faster than comparing a String to its upper-case version as it requires only 1 pass:

public static boolean isUpper(String s)
{
    for(char c : s.toCharArray())
    {
        if(! Character.isUpperCase(c))
            return false;
    }

    return true;
}

Please note that there might be some localization issues with different character sets. I don't have any first hand experience but I think there are some languages (like Turkish) where different lower case letters can map to the same upper case letter.

Try this, may help.

import java.util.regex.Pattern;

private static final String regex ="^[A-Z0-9]"; //alpha-numeric uppercase
public static boolean isUpperCase(String str){
    return Pattern.compile(regex).matcher(str).find();
}

with this code, we just change the regex.

I realise that this question is quite old, but the accepted answer uses a deprecated API, and there's a question about how to do it using ICU4J. This is how I did it:

s.chars().filter(UCharacter::isLetter).allMatch(UCharacter::isUpperCase)

If you expect your input string to be short, you could go with myString.toUpperCase().equals(myString) as you suggested. It's short and expressive.

But you can also use streams:

boolean allUpper = myString.chars().noneMatch(Character::isLowerCase);

You can use java.lang.Character.isUpperCase() Then you can easily write a method that check if your string is uppercase (with a simple loop).

Sending the message toUpperCase() to your string and then checking if the result is equal to your string will be probably slower.

Here's a solution I came up with that's a bit universal as it doesn't require any libraries or special imports, should work with any version of Java, requires only a single pass, and should be much faster than any regex based solutions:

public static final boolean isUnicaseString(String input) {
  char[] carr = input.toCharArray();
  // Get the index of the first letter
  int i = 0;
  for (; i < carr.length; i++) {
    if (Character.isLetter(carr[i])) {
      break;
    }
  }
  // If we went all the way to the end above, then return true; no case at all is technically unicase
  if (i == carr.length) {
    return true;
  }
  // Determine if first letter is uppercase
  boolean firstUpper = Character.isUpperCase(carr[i]);
  for (; i < carr.length; i++) {
    // Check each remaining letter, stopping when the case doesn't match the first
    if (Character.isLetter(carr[i]) && Character.isUpperCase(carr[i]) != firstUpper) {
      return false;
    }
  }
  // If we didn't stop above, then it's unicase
  return true;
}
Related