Check if a String Contains only Alphabets in Java using Regex Last Updated : 05 Apr, 2022 Comments Improve Suggest changes Like Article Like Report 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. For any string, here the task is to check whether a string contains only alphabets or not using Regex. Now for a given string, the characters of the string are checked one by one using Regex. Regex 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. ^[a-zA-Z]*$ Illustrations: Input: GeeksforGeeks Output: True Input: Geeks4Geeks Output: False Input: null Output: FalseAlgorithmGet the stringMatch the string with the Regex using matches().Return true is matched Pseudocode for the above algorithm is proposed below as follows: public static boolean isStringOnlyAlphabet(String str) { return ((!str.equals("")) && (str != null) && (str.matches("^[a-zA-Z]*$"))); } Example: Java // Java Program to Check If String Contains Only Alphabets // Using Regular Expression // Main class class GFG { // Method 1 // To check String for only Alphabets public static boolean isStringOnlyAlphabet(String str) { return ((str != null) && (!str.equals("")) && (str.matches("^[a-zA-Z]*$"))); } // Method 2 // Main driver method public static void main(String[] args) { // Calling out methods over string // covering all scenarios // Use case 1 System.out.println("Test Case 1:"); // Input string String str1 = "GeeksforGeeks"; System.out.println("Input: " + str1); System.out.println("Output: " + isStringOnlyAlphabet(str1)); // Use case 2 // Checking for String with numeric characters System.out.println("\nTest Case 2:"); // Input string String str2 = "Geeks4Geeks"; System.out.println("Input: " + str2); System.out.println("Output: " + isStringOnlyAlphabet(str2)); // Use Case 3 // Checking for null String System.out.println("\nTest Case 3:"); // Input string String str3 = null; System.out.println("Input: " + str3); System.out.println("Output: " + isStringOnlyAlphabet(str3)); // Use Case 4 // Checking for empty String System.out.println("\nTest Case 4:"); // Input string String str4 = ""; System.out.println("Input: " + str4); System.out.println("Output: " + isStringOnlyAlphabet(str4)); } } Output: 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 valuesCheck if a string contains only alphabets in Java using Lambda expression Comment More infoAdvertise with us Next Article Check if a String Contains only Alphabets in Java using Regex C code_r Follow Improve Article Tags : Java Java-Strings java-regular-expression Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads Check if a String Contains only Alphabets in Java In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.Example: The most common and straightforward approach to validate if 4 min read Check if a String Contains Only Alphabets in Java Using Lambda Expression Lambda expressions basically express instances of functional interfaces (An interface with a single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces. Given a String 3 min read Extracting each word from a String using Regex in Java Given a string, extract words from it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Funny?? are not you? Output : Funny are not you Input : Geeks for geeks?? Output : Geeks for geeks We have discussed a solut 2 min read Check if a string consists only of special characters Given string str of length N, the task is to check if the given string contains only special characters or not. If the string contains only special characters, then print âYesâ. Otherwise, print âNoâ. Examples: Input: str = â@#$&%!~âOutput: YesExplanation: Given string contains only special char 9 min read Get the first letter of each word in a string using regex in Java Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeks Output :Gfg Input : United Kingdom Output : UK Below is the Regular expression to extr 1 min read Print first letter of each word in a string using regex Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeksOutput :Gfg Input : United KingdomOutput : UKBelow is the Regular expression to extract 3 min read Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach) In Set 1, we have discussed general approach to check whether a string is a valid number or not. In this post, we will discuss regular expression approach to check for a number. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 3 min read How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or 3 min read Charset contains() method in Java with Examples The contains() method is a built-in method of the java.nio.charset checks if a given charset is in another given charset. A charset X contains a charset Y if every character representable in Y is also representable in X. Every charset contains itself. However, this method computes an approximation o 2 min read Java String contains() Method with Example The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java.Example:In this example, we check if a specific substring is present in the given string.Java// Java Program to 3 min read Like