How to Check String is Alphanumeric in Java

You can check string is alphanumeric in Java using matches() method of Matcher class. The Matcher class is provided by java.util.regex package. Below I have shared a simple Java program in which I have taken a string and I am checking it using matches() method.

 

Java Program to Check String is Alphanumeric or not

 

Output

How to Check String is Alphanumeric in Java

In above example, I have used a pattern “[a-zA-Z0-9]+” inside matches() method. It means that the string can contains characters in between a to z, A to Z and 0 to 9. Here + means string can have one or more characters. matches() method returns true if the string is alphanumeric, otherwise it returns false.

Comment below if you are getting difficulty to understand anything.

 

Leave a Reply

Your email address will not be published. Required fields are marked *