Check if a String Contains Only Alphabets in Java Using Lambda Expression

Last Updated : 20 Feb, 2026

Lambda Expressions in Java provide a concise way to represent a method using an expression instead of a full method definition. Validating strings to ensure they contain only alphabets becomes simple and readable with them.

Problem Statement

We need to check whether a given string contains only Alphabet letters.

  • Alphabets include A-Z and a-z only.
  • Strings with numbers, spaces, or special characters are considered invalid.

Input: HelloWorld
Output: true

Input: Java123
Output: false

Input: Hello World
Output: false

Approach

  • Convert the input string into a stream of characters using chars().
  • Use allMatch() with Character::isLetter to check if every character is an alphabet.
  • Store the result in a boolean variable.
  • Print the boolean result (true if all letters, otherwise false).
Java
public class GFG{
    
    public static void main(String[] args){
        
        // Input string
        String input = "HelloWorld";

        // Check if all characters are alphabets
        boolean isAlpha = input.chars().allMatch(Character::isLetter);

        // Print true or false
        System.out.println(isAlpha);
    }
}

Output
true

Related Articles: 

Comment