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

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set