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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 20-Sep-2019 08:17:39
Advertisements