![]() |
VOOZH | about |
In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.
Example:
The most common and straightforward approach to validate if a string contains only alphabetic characters is the regular expression. It validates the string by matching it to a specific pattern.
true false false
Explanation: In the above program, the isAlphabetic() method checks if the given string contains only alphabetic characters using the regex [a-zA-Z]+.
Table of Content
This method checks each character in a string to make sure it falls within the ASCII range for uppercase (65–90) or lowercase (97–122) letters.
true false false
We can use the modern approach Lambda expressions and Streams of Java 8 to check if all characters in the string are alphabetic.
true false false
Explanation: In the above example, the s.chars() converts the string to an IntStream of Unicode code points. The allMatch(Character::isLetter) checks every character is a letter.
This approach uses the built-in Character.isLetter() of Character class to validate each character.
true false false
Explanation: In the above example, the Character.isLetter() method checks if a character is a letter and it supports Unicode alphabets as well.