How to check if a String has only alphabets
Posted On
Posted By admin
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.
If you like this post, please do let me know via the comments box below. You can also connect with me via my Facebook Page or subscribe to my Youtube channel!