
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements