There are following way to check if a String Contains only Alphabets in Java.
- We take a string word.
- Declare A method checkAlphabet() to check if a String Contains only Alphabets in Java.
- Inside that method use regular expression regex [A-Za-z]*$ to validate String contain only alphabetic characters.
- Now return the Boolean status to print output.
public class TestAlphabet {
public static void main(String[] args) {
String str = "Welcome";
boolean status = checkAlphabet(str);
System.out.println("String contain only alphabet:- " + status);
}
public static boolean checkAlphabet(String str) {
return (!str.isEmpty() && str != null && str.matches("[A-Za-z]*$"));
}
}
Output:-
String contain only alphabet:- true