VOOZH about

URL: https://www.geeksforgeeks.org/java/check-if-a-string-contains-only-alphabets-in-java/

⇱ Check if a String Contains only Alphabets in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


Output
true
false
false

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.


Output
true
false
false


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.


Output
true
false
false

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.


Output
true
false
false

Explanation: In the above example, the Character.isLetter() method checks if a character is a letter and it supports Unicode alphabets as well.

Comment