tags:

views:

978

answers:

6

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.

+4  A: 

The easy way would be myString.toUpperCase().equals(myString). If this code isn't a bottleneck, that may be all you need to do.

You could also use a Pattern, like myString.matches("\\p{javaUppercase}"). This class behaves just like Character.isUpperCase(), according to the docs.
Don't just check if all characters are upper-case; this doesn't work if there are numbers or symbols in the string, and it could also cause internationalization problems.

EDIT: The only existing method I could find is this one, which doesn't seem to be in a free library.

Michael Myers
But have you ever seen any utility method for this in any of the more common frameworks?
Nicolai
this is one or two lines of code. why you want to have a util method.
Markus Lausberg
As there are already 5 different suggestions on this page on how to do this, and I was hoping someone else had already figured out the best and most efficient way of doing this.
Nicolai
Outlaw Programmer's solution is most likely the fastest. As I said, though, unless this is a bottleneck, it isn't likely to matter. But if you're going to make it part of a personal library, then it is indeed a good idea to make it fast.
Michael Myers
The myString.toUpperCase().equals(myString) method is a safe mechanism for doing the check - do this and you're less likely to fall afoul of any Unicode/locale issues.
McDowell
@McDowell: Glad to earn your approval (and I say that in all seriousness).
Michael Myers
After having run into some of the problems mentioned; i have decided that your way is probably the safest. Take a look at my latest EDIT in the question.
Nicolai
+2  A: 

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.

Markus Lausberg
+1  A: 

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.

Outlaw Programmer
Note that toCharArray() will create a copy of the string. I suggest to use the old chatAt() instead.
Aaron Digulla
In Turkish upper and lower case are as usual, it's just that the mapping is different from every other locale. Specifically whether "i" has an umlaut switches when switching case. The German eszet transforms to two characters when made upper case.
Tom Hawtin - tackline
If the string contains non-letter characters (such as "2"), this method isn't very useful. It probably doesn't work very well with letters from alphabets that don't have upper/lower cases or supplementary range characters (32bit codepoints).
McDowell
A: 

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.

SmuK
+1  A: 

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;
}
Simon Nickerson
+1 for not allocating objects
Aaron Digulla
A: 

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.