Skip to content

Check if String is lowercase in Java example

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

How to check if the string is lowercase in Java?

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

We can check by converting string to a character array and using the isLowerCase method of the Character class as given below.

Output

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

Output

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

2) Using toLowerCase and equals methods

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

Output

3) Using the Apache Commons library

If you are using the Apache Commons library, you can use the isAllLowerCase method of the StringUtils class to check if the string is lowercase as given below.

Output

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

This example is a part of the Java String tutorial.

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

About the author

Leave a Reply

Your email address will not be published.