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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javacodeexamples.stringexamples; public class StringCheckIfLowerCaseExample { public static void main(String[] args) { String str = "lowercasestring"; System.out.println( "Is String lowercase?: " + isStringLowerCase(str) ); } private static boolean isStringLowerCase(String str){ //convert String to char array char[] charArray = str.toCharArray(); for(int i=0; i < charArray.length; i++){ //if any character is not in lower case, return false if( !Character.isLowerCase( charArray[i] )) return false; } return true; } } |
Output
1 |
Is String lowercase?: true |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javacodeexamples.stringexamples; public class StringCheckIfLowerCaseExample { public static void main(String[] args) { String str = "james bond, 007"; System.out.println( "Is String lowercase?: " + isStringLowerCase(str) ); } private static boolean isStringLowerCase(String str){ //convert String to char array char[] charArray = str.toCharArray(); for(int i=0; i < charArray.length; i++){ //if the character is a letter if( Character.isLetter(charArray[i]) ){ //if any character is not in lower case, return false if( !Character.isLowerCase( charArray[i] )) return false; } } return true; } } |
Output
1 |
Is String lowercase?: true |
You can also use the isUpperCase
method instead of the isLowerCase
to address the non-letter character problem as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private static boolean isStringLowerCase(String str){ //convert String to char array char[] charArray = str.toCharArray(); for(int i=0; i < charArray.length; i++){ //if any character is in upper case, return false if( Character.isUpperCase( charArray[i] )) return false; } return true; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.stringexamples; public class StringCheckIfLowerCaseExample { public static void main(String[] args) { String str = "lowercasestring"; if( str.equals(str.toLowerCase()) ) System.out.println("String is in lower case"); else System.out.println("String is not in lower case"); } } |
Output
1 |
String is in lower case |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class StringCheckIfLowerCaseExample { public static void main(String[] args) { String str = "lowercasestring"; System.out.println( StringUtils.isAllLowerCase(str) ); } } |
Output
1 |
true |
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.