Capitalize the First Letter of a String in Java
In Java, to check if a String contains only uppercase letters, we can use many approaches. These approaches verify each character in the String to ensure they are all in uppercase. A simple method, such as iterating through the characters and checking their case.
In this article, we will discuss how to check if a String contains only uppercase letters in Java.
Methods to check if a String contains only uppercase letters
Below are the three methods to check if the String contains only uppercase letters or not in Java.
Program to check if a String contains only uppercase letters in Java
Below are the implementations of the above-mentioned methods to check if a String contains only uppercase letters or not.
Method 1: string.matches()
The string.matches() method in Java proves invaluable for verifying whether a given string conforms to a predefined regular expression. This functionality is particularly useful when aiming to ascertain if a string exclusively comprises uppercase letters.
Java
// Java Program to Check if String Contains Only Uppercase Letters public class Main { public static void main(String[] args) { // Sample string String string = "w3wiki" ; System.out.println( "Method 1: string.matches()" ); // to check if the string contains only uppercase letters if (string.matches( "[A-Z]+" )) { System.out.println( "The string contains only uppercase letters." ); } else { System.out.println( "The string contains lowercase letters or non-alphabetic characters." ); } } } |
Method 1: string.matches() The string contains only uppercase letters.
Explanation of the above Program:
- This Java program checks if a given string contains only uppercase letters.
- It uses the
matches("[A-Z]+")
method, which checks if the entire string consists of one or more uppercase letters. - In simpler terms, it examines whether a word is entirely composed of capital letters or not.
Method 2: Apache Commons Lang library
It is easy method for checking the string is uppercase or not.
Java
// Java Program to Check if String Contains Only Uppercase Letters using Apache Commons Lang import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { // Sample string String string = "w3wiki" ; System.out.println( "Method 2: Apache Commons Lang library " ); // checks if the string contains only uppercase letters using Apache Commons Lang if (StringUtils.isAllUpperCase(string)) { System.out.println( "The string contains only uppercase letters." ); } else { System.out.println( "The string contains lowercase letters or non-alphabetic characters." ); } } } |
Output:
Method 2: Apache Commons Lang library
The string contains only uppercase letters.
Explanation of the above Program:
- This Java program checks if a given string contains only uppercase letters using Apache Commons Lang library.
- It utilizes the
StringUtils.isAllUpperCase(string)
method to determine if the string consists exclusively of uppercase letters. - In simple, it uses a specialized library to check if all the letters in a word are written in capital letters.
Method 3: Character.isUpperCase()
The core mechanism involves checking each character with the Character.isUpperCase() method in Java, which returns true if the character is an uppercase letter.
Java
// Java Program to Check if String Contains Only Uppercase Letters without External Libraries public class Main { public static void main(String[] args) { // Sample string String string = "GEEKFORBeginner" ; boolean containsOnlyUppercase = true ; System.out.println( "Method 3: Character.isUpperCase() " ); // checks if the string contains only uppercase letters for ( char ch : string.toCharArray()) { if (!Character.isUpperCase(ch)) { containsOnlyUppercase = false ; break ; } } // Display the result if (containsOnlyUppercase) { System.out.println( "The string contains only uppercase letters." ); } else { System.out.println( "The string contains lowercase letters or non-alphabetic characters." ); } } } |
Method 3: Character.isUpperCase() The string contains only uppercase letters.
Explanation of the above Program:
- This Java program checks if a given string contains only uppercase letters without using external libraries.
- It takes a sample string, “GEEKFORBeginner,” and iterates through each character using a loop.
- The
Character.isUpperCase(ch)
method is employed to verify if each character is uppercase. - If any lowercase letter or non-alphabetic character is found, it sets a boolean variable
containsOnlyUppercase
to false, breaking out of the loop. - Finally, it prints whether the string contains only uppercase letters or not.
- In simpler terms, it manually examines each character to ensure they are all in capital letters.