Java String Handling

Check if String is lowercase in Java example

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

How to check if String is lowercase in Java?

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

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

We can check if String is lowercase using character array and isLowerCase method of 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 string contains numbers, spaces and a comma for which isLowerCase method returns false. To fix that, before checking if the character is lowercase, we need to first check if it is letter as given below.

Output

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

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

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

Output

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

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.