Skip to content

Check if String is uppercase in Java example

This Java example shows how to check if String is uppercase in Java using various approaches including a char array, character class, and apache commons.

How to check if string is uppercase in Java?

1) Check if the string is uppercase using a char array and the Character class

We can first covert string to a character array and then use the isUpperCase method of the Character class as given below.

Output

What about the input string “STRING123, TEST”? Well, our program will output false for this input value. That is because string contains numbers, space and a comma for which the isUpperCase method returns false. To fix that, before checking if the character is uppercase, we need to first check if it is a letter as given below.

Output

You can also use the isLowerCase method instead of the isUpperCase to address non-letter character problem as given below.

2) Using the toUpperCase and equals methods

We can convert the string to uppercase and compare it with the original string. If they are equal then the string is in uppercase as given below.

Output

3) Using Apache Commons library

If you are using Apache Commons library, you can use the isAllUpperCase method of the StringUtils class to check if the String is uppercase as given below.

Output

Note: If the string contains any non-letter characters including space, the isAllUpperCase method returns false.

This example is a part of the Java String tutorial with examples.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.