Check if a string contains only alphabets in Java using Lambda expression



Let’s say our string is −

String str = "Amit123";

Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −

boolean result = str.chars().allMatch(Character::isLetter);

Following is an example to check if a string contains only alphabets using Lambda Expressions −

Example

class Main {    public static void main(String[] args) {       String str = "Amit123";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }

Output

Let us see another example with a different input −

String contains only alphabets? = false

Example

class Main {    public static void main(String[] args) {       String str = "Jacob";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }

Output

String contains only alphabets? = true
Updated on: 2019-09-20T08:19:04+05:30

789 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements