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.
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 StringCheckIfUpperCaseExample { public static void main(String[] args) { String str = "UPPERCASESTRING"; System.out.println( "Is String uppercase?: " + isStringUpperCase(str) ); } private static boolean isStringUpperCase(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 upper case, return false if( !Character.isUpperCase( charArray[i] )) return false; } return true; } } |
Output
1 |
Is String uppercase?: true |
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.
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 32 |
package com.javacodeexamples.stringexamples; public class StringCheckIfUpperCaseExample { public static void main(String[] args) { String str = "STRING123, TEST"; System.out.println( "Is String uppercase?: " + isStringUpperCase(str) ); } private static boolean isStringUpperCase(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 upper case, return false if( !Character.isUpperCase( charArray[i] )) return false; } } return true; } } |
Output
1 |
Is String uppercase?: true |
You can also use the isLowerCase
method instead of the isUpperCase
to address non-letter character problem as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private static boolean isStringUpperCase(String str){ //convert String to char array char[] charArray = str.toCharArray(); for(int i=0; i < charArray.length; i++){ //if any character is in lower case, return false if( Character.isLowerCase( charArray[i] )) return false; } return true; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.stringexamples; public class StringCheckIfUpperCaseExample { public static void main(String[] args) { String str = "UPPERCASE STRING"; if( str.equals(str.toUpperCase()) ) System.out.println("String is uppercase"); else System.out.println("String is not uppercase"); } } |
Output
1 |
String is uppercase |
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.
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 StringCheckIfUpperCaseExample { public static void main(String[] args) { String str = "UPPERCASESTRING"; System.out.println( StringUtils.isAllUpperCase(str) ); } } |
Output
1 |
true |
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.