Java String Handling

Check if String is uppercase in Java example

This Java example shows how to check if String is uppercase in Java. Example also shows how to check if String is uppercase using various approaches.

How to check if String is uppercase in Java?

We want to check if String is uppercase. This can be done in below given approaches.

1) Check if String is uppercase using char array and Character class

We can check if String is uppercase using character array and isUpperCase method of 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 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 isLowerCase method instead of isUpperCase to address non-letter character problem as given below.

2) Check if String is uppercase using 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) Check if String is uppercase using Apache Commons library

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

Output

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

Visit String examples to learn more. Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *