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.
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 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 isLowerCase
method instead of 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) 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.
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) 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.
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 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.
Leave a Reply