Java ProgramsStringsCheck String Contains Only Alphabets

Check String Contains Only Alphabets in Java

beginner·  Strings  ·  String

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.

Input
HelloWorld
Output
Contains only alphabets: true

Java Program

Java
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

Contains only alphabets: true

Core Logic

Checking every character against Character.isLetter(), and stopping the instant one fails, confirms whether the whole string is made of letters.

How It Works
  1. 1onlyAlphabets starts as true, assuming the string qualifies until proven otherwise.
  2. 2A for-each loop visits each character of the string in turn.
  3. 3Character.isLetter(c) checks whether the current character is a letter, in any case.
  4. 4The first character that fails the check sets onlyAlphabets to false and exits the loop immediately with break.
For "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().

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

Character.isLetter()for-each loopearly exit with break

Approach 2: Using matches()

Java
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

Contains only alphabets: true

Core Logic

A regex pattern can express 'one or more letters, and nothing else' directly, checking the whole string in a single call.

How It Works
  1. 1str.matches("[a-zA-Z]+") checks the pattern against the string.
  2. 2matches() requires the pattern to cover the ENTIRE string, not just part of it.
  3. 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

regexString.matches()

Approach 3: Java 8

Java
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

Contains only alphabets: true

Core Logic

The same per-character check can ask a stream directly — does every character satisfy Character.isLetter()?

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.allMatch(Character::isLetter) checks that every single code passes the letter test.
  3. 3allMatch() returns true only if every character qualifies, and stops at the first one that doesn't.
For "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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: allMatch() stops as soon as it finds a non-letter, the same short-circuiting behavior as the loop's break.

Key Concepts

Streamchars()allMatch()

Related Programs