Check if a String Contains only Alphabets in Java
Last Updated :
15 Jul, 2025
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 a string contains only alphabetic characters is the regular expression. It validates the string by matching it to a specific pattern.
Java
// Java program to check if a string
// contains only alphabets using Regex
public class CheckAlphabets {
// Method to check if a string is alphabetic
public static boolean isAlphabetic(String s) {
// Return false if the string is null or empty
if (s == null || s.isEmpty()) {
return false;
}
// Match the string with the regex [a-zA-Z]+
// This checks if the string contains
// only alphabetic characters
return s.matches("[a-zA-Z]+");
}
public static void main(String[] args) {
// Test cases with different inputs
System.out.println(isAlphabetic("GeeksforGeeks"));
System.out.println(isAlphabetic("Geeks2024"));
System.out.println(isAlphabetic(""));
}
}
Explanation: In the above program, the isAlphabetic() method
checks if the given string contains only alphabetic characters using the regex [a-zA-Z]+
.
Other Methods to Check if a String Contains only Alphabets
1. Using ASCII Values
This method checks each character in a string to make sure it falls within the ASCII range for uppercase (65–90
) or lowercase (97–122
) letters.
Java
// Java program to check if a string
// contains only alphabets Using ASCII range
public class CheckAlphabets {
public static boolean isAlphabetic(String s) {
// Return false if the string is
// null or empty
if (s == null || s.isEmpty()) {
return false;
}
// Iterate through each character in the string
for (char c : s.toCharArray()) {
// Check if the character is not a letter
// (between A-Z or a-z in ASCII range)
if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) {
// Return false if any non-alphabet character is found
return false;
}
}
return true; // Return true if all characters are alphabetic
}
public static void main(String[] args) {
System.out.println(isAlphabetic("GeeksforGeeks"));
System.out.println(isAlphabetic("Geeks2024"));
System.out.println(isAlphabetic(""));
}
}
2. Using Lambda Expressions
We can use the modern approach Lambda expressions and Streams of Java 8 to check if all characters in the string are alphabetic.
Java
// Java program to check if a string contains only alphabets
// using Lambda expression and streams
import java.util.stream.IntStream;
public class CheckAlphabets {
public static boolean isAlphabetic(String s) {
// Return false if the string
// is null or empty
if (s == null || s.isEmpty()) {
return false;
}
// Use streams to check if all characters
// in the string are alphabet
return s.chars().allMatch(Character::isLetter);
}
public static void main(String[] args) {
System.out.println(isAlphabetic("GeeksforGeeks"));
System.out.println(isAlphabetic("Geeks2024"));
System.out.println(isAlphabetic(""));
}
}
Explanation: In the above example, the s.chars()
converts the string to an IntStream of Unicode code points. The allMatch(Character::isLetter)
checks every character is a letter.
3. Using the Character Class
This approach uses the built-in Character.isLetter()
of Character class to validate each character.
Java
// Java program to check if a string contains only alphabets
// using Character class
public class CheckAlphabets {
public static boolean isAlphabetic(String s) {
// Return false if the string
// is null or empty
if (s == null || s.isEmpty()) {
return false;
}
// Check each character
// using Character.isLetter()
for (char c : s.toCharArray()) {
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isAlphabetic("GeeksforGeeks"));
System.out.println(isAlphabetic("Geeks2024"));
System.out.println(isAlphabetic(""));
}
}
Explanation: In the above example, the Character.isLetter()
method checks if a character is a letter and it supports Unicode alphabets as well.
Explore
Basics
OOPs & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java