How to check if a String has only alphabets
You can use Java regular expressions to check if a String has only alphabets as follows:
package demo;public class StringDemo {public static void main(String[] args) {String str = "HelloWorld";String regex = "^[a-zA-Z]+$";boolean matches = str.matches(regex);System.out.println(matches);}}
The above code will print true when you run it. If you change the String str to have any other characters like numbers or special characters, it will print false.
Comments
Post a Comment