Given a string, the task is to check whether a string contains only alphabets or not using Regex in Java.
Examples:
Input: GeeksforGeeks Output: True Input: Geeks4Geeks Output: False Input: null Output: False
In this article, the characters of the string are checked one by one using Regex.
Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package.
The Regex
^[a-zA-Z]*$
can be used to check a string for alphabets. String.matches() method is used to check whether or not the string matches the given regex.
Algorithm:
- Get the string
- Match the string with the Regex using matches().
- Return true is matched
Pseudocode:
public static boolean isStringOnlyAlphabet(String str) { return ((!str.equals("")) && (str != null) && (str.matches("^[a-zA-Z]*$"))); } |
Below is the implementation of the above approach:
Program: Checking for String containing only Alphabets
// Java program to check if String contains only Alphabets // using Regular Expression class GFG { // Function to check String for only Alphabets public static boolean isStringOnlyAlphabet(String str) { return ((str != null) && (!str.equals("")) && (str.matches("^[a-zA-Z]*$"))); } // Main method public static void main(String[] args) { // Checking for True case System.out.println("Test Case 1:"); String str1 = "GeeksforGeeks"; System.out.println("Input: " + str1); System.out.println( "Output: " + isStringOnlyAlphabet(str1)); // Checking for String with numeric characters System.out.println("\nTest Case 2:"); String str2 = "Geeks4Geeks"; System.out.println("Input: " + str2); System.out.println( "Output: " + isStringOnlyAlphabet(str2)); // Checking for null String System.out.println("\nTest Case 3:"); String str3 = null; System.out.println("Input: " + str3); System.out.println( "Output: " + isStringOnlyAlphabet(str3)); // Checking for empty String System.out.println("\nTest Case 4:"); String str4 = ""; System.out.println("Input: " + str4); System.out.println( "Output: " + isStringOnlyAlphabet(str4)); } } |
Test Case 1: Input: GeeksforGeeks Output: true Test Case 2: Input: Geeks4Geeks Output: false Test Case 3: Input: null Output: false Test Case 4: Input: Output: false
Related Articles:
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java using Lambda expression
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java
- Program to check if a String in Java contains only whitespaces
- How to check if string contains only digits in Java
- Extracting each word from a String using Regex in Java
- Get the first letter of each word in a string using regex in Java
- Print first letter of each word in a string using regex
- Java | Date format validation using Regex
- Java | Removing whitespaces using Regex
- Count occurrences of a given character using Regex in Java
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- How to validate an IP address using ReGex
- Regex Boundary Matchers in Java
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Check if a string consists only of special characters
- Java String contains() method with example
- LinkedBlockingDeque contains() method in Java
- HashSet contains() Method in Java
- Arraylist.contains() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

