Check String Contains Only Alphabets in Java
Problem
A string contains only alphabets when every single character in it is a letter, with no digits, spaces, or punctuation mixed in.
Given a string, determine whether it consists entirely of letter characters.
Java Program
public class OnlyAlphabetsCheck {
public static void main(String[] args) {
String str = "HelloWorld";
boolean onlyAlphabets = true;
for (char c : str.toCharArray()) {
if (!Character.isLetter(c)) {
onlyAlphabets = false;
break; // found a non-letter, no need to keep checking
}
}
System.out.println("Contains only alphabets: " + onlyAlphabets);
}
}Output
Core Logic
Checking every character against Character.isLetter(), and stopping the instant one fails, confirms whether the whole string is made of letters.
- 1
onlyAlphabetsstarts astrue, assuming the string qualifies until proven otherwise. - 2A for-each loop visits each character of the string in turn.
- 3
Character.isLetter(c)checks whether the current character is a letter, in any case. - 4The first character that fails the check sets
onlyAlphabetstofalseand exits the loop immediately withbreak.
"HelloWorld", every character passes Character.isLetter(), so the loop finishes with onlyAlphabets still true.Key Point: This is the mirror image of the digits-only check — same loop structure and early-exit logic, just testing isLetter() instead of isDigit().
Why: Each character is checked once, and the loop exits at the first non-letter found, without allocating anything beyond a boolean flag.
Key Concepts
Approach 2: Using matches()
public class OnlyAlphabetsCheckRegex {
public static void main(String[] args) {
String str = "HelloWorld";
// matches() requires the pattern to cover the entire string, not just part of it
boolean onlyAlphabets = str.matches("[a-zA-Z]+");
System.out.println("Contains only alphabets: " + onlyAlphabets);
}
}
Output
Core Logic
A regex pattern can express 'one or more letters, and nothing else' directly, checking the whole string in a single call.
- 1
str.matches("[a-zA-Z]+")checks the pattern against the string. - 2
matches()requires the pattern to cover the ENTIRE string, not just part of it. - 3
[a-zA-Z]+means 'one or more letters, upper or lower case', so any digit, space, or symbol anywhere causes the whole match to fail.
"HelloWorld".matches("[a-zA-Z]+") returns true, since every character is a letter and the pattern covers the whole string.Key Point: A string with a space in it, like "Hello World", would fail this check — the space isn't in the [a-zA-Z] character class, matching the loop version's behavior.
Why: matches() still scans every character against the pattern, but compiling and running the regex adds overhead a plain loop doesn't have.
Key Concepts
Approach 3: Java 8
public class OnlyAlphabetsCheckStream {
public static void main(String[] args) {
String str = "HelloWorld";
// allMatch() short-circuits at the first character that fails the check
boolean onlyAlphabets = str.chars().allMatch(Character::isLetter);
System.out.println("Contains only alphabets: " + onlyAlphabets);
}
}
Output
Core Logic
The same per-character check can ask a stream directly — does every character satisfy Character.isLetter()?
- 1
str.chars()returns anIntStreamof the string's character codes. - 2
.allMatch(Character::isLetter)checks that every single code passes the letter test. - 3
allMatch()returnstrueonly if every character qualifies, and stops at the first one that doesn't.
"HelloWorld", allMatch(Character::isLetter) checks all ten characters and finds no non-letter, so it returns true.Key Point: Swapping Character::isLetter for Character::isDigit is the only change needed to turn this into the digits-only check — the rest of the pipeline stays identical.
Why: allMatch() stops as soon as it finds a non-letter, the same short-circuiting behavior as the loop's break.