Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

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.

like image 557
Nicolai Avatar asked Mar 24 '09 14:03

Nicolai


People also ask

How do you check if a string is all uppercase Java?

The java. lang. Character. isUpperCase(char ch) determines if the specified character is an uppercase character.

What string method can be used to check if all characters are uppercase?

To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.

How do you know if a string is upper or lower case?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do you check if a string contains only uppercase letters?

We can check if a string contains only upper case letters using 2 methods. First is using method isupper().


2 Answers

Now in StringUtils isAllUpperCase

like image 196
gerardw Avatar answered Oct 12 '22 03:10

gerardw


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; } 
like image 43
Simon Nickerson Avatar answered Oct 12 '22 01:10

Simon Nickerson