Check if a String contains alphanumeric characters in Java
This post will explore different ways to check if a string contains alphanumeric characters in Java. In other words, determine whether a string consists of only numbers and alphabets. A null string should return false, and an empty string should return true.
Java did not provide any standard method for this simple task. Nevertheless, there are several methods to detect if the given string is alphanumeric in Java:
1. Using Regular Expression
The idea is to use the regular expression ^[a-zA-Z0-9]*$, which checks the string for alphanumeric characters. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static boolean isAlphaNumeric(String s) { return s != null && s.matches("^[a-zA-Z0-9]*$"); } public static void main(String[] args) { String s = "ABC"; System.out.println("IsAlphaNumeric: " + isAlphaNumeric(s)); } } |
Output:
IsAlphaNumeric: true
If the regular expression is frequently called, you might want to compile the regular expression for performance boost:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.regex.Pattern; class Main { private static Pattern p = Pattern.compile("^[a-zA-Z0-9]*$"); public static boolean isAlphaNumeric(String s) { return p.matcher(s).find(); } public static void main(String[] args) { String s = "ABC"; System.out.println("IsAlphaNumeric: " + isAlphaNumeric(s)); } } |
Output:
IsAlphaNumeric: true
2. Using Lambda Expressions
From Java 8 onwards, this can be efficiently done using lambda expressions:
|
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String[] args) { String s = "ABC12"; boolean isAlphaNumeric = s != null && s.chars().allMatch(Character::isLetterOrDigit); System.out.println("IsAlphaNumeric: " + isAlphaNumeric); } } |
Output:
IsAlphaNumeric: true
3. Using External Libraries
We can use the Apache Commons Lang library, a method called isAlphanumeric() included in the StringUtils class.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String s = "ABC12"; System.out.println("IsAlphaNumeric: " + StringUtils.isAlphanumeric(s)); } } |
4. Naive Solution
In plain Java, we can iterate over the string characters and check each character to be alphanumeric using Character.isLetterOrDigit(char). This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Main { public static Boolean isAlphaNumeric(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9')) { return Boolean.FALSE; } } return Boolean.TRUE; } public static void main(String[] args) { String s = "ABC12"; System.out.println("IsAlphaNumeric: " + isAlphaNumeric(s)); } } |
Output:
IsAlphaNumeric: true
That’s all about determining whether a String contains alphanumeric characters in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)