Using Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

Alternativly iterate over String's characters and check with:

!Character.isLetterOrDigit(char)

You've still one problem left: Your example string "abcdefà" is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?!

So you may want to use regular expression instead:

String s = "abcdefà";
Pattern p = Pattern.compile("[^a-zA-Z0-9]");
boolean hasSpecialChar = p.matcher(s).find();
Answer from Fabian Barney on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › check if a string is strictly alphanumeric with java
Check if a String Is Strictly Alphanumeric With Java | Baeldung
October 9, 2023 - We can implement isAlphanumeric(int) in several ways, but overall, we must match the character code in the ASCII table.
Discussions

performance - Fastest way to check a string is alphanumeric in Java - Stack Overflow
What is the fastest way to check that a String contains only alphanumeric characters. I have a library for processing extremely large data files, that is CPU bound. I am explicitly looking for More on stackoverflow.com
🌐 stackoverflow.com
java - Regex for checking if a string is strictly alphanumeric - Stack Overflow
How can I check if a string contains only numbers and alphabets ie. is alphanumeric? More on stackoverflow.com
🌐 stackoverflow.com
How to list all alphanumeric UTF-8 characters?
The official comprehensive table : https://www.unicode.org/Public/UCD/latest/ More on reddit.com
🌐 r/cpp_questions
10
1
October 2, 2022
How do you test if there is alphanumeric characters in a string?
Have not tested thoroughly, but something like this: var regex = RegEx.new() regex.compile("[^A-Za-z0-9]") var result = regex.search("0123_ABcd$0123") if result: print("Alphanumeric characters only! Found: [", result.get_string(0), "]") More on reddit.com
🌐 r/godot
8
6
May 14, 2021
🌐
Educative
educative.io › answers › what-is-stringutilsisalphanumeric-in-java
What is StringUtils.isAlphanumeric in Java?
isAlphanumeric() is a static method of the StringUtils class that is used to check if the given string only contains Unicode letters or digits. The method returns false if the string contains Unicode symbols other than Unicode letters and digits ...
Top answer
1 of 3
52

Use String.matches(), like:

String myString = "qwerty123456";
System.out.println(myString.matches("[A-Za-z0-9]+"));

That may not be the absolute "fastest" possible approach. But in general there's not much point in trying to compete with the people who write the language's "standard library" in terms of performance.

2 of 3
34

I've written the tests that compare using regular expressions (as per other answers) against not using regular expressions. Tests done on a quad core OSX10.8 machine running Java 1.6

Interestingly using regular expressions turns out to be about 5-10 times slower than manually iterating over a string. Furthermore the isAlphanumeric2() function is marginally faster than isAlphanumeric(). One supports the case where extended Unicode numbers are allowed, and the other is for when only standard ASCII numbers are allowed.

public class QuickTest extends TestCase {

    private final int reps = 1000000;

    public void testRegexp() {
        for(int i = 0; i < reps; i++)
            ("ab4r3rgf"+i).matches("[a-zA-Z0-9]");
    }

public void testIsAlphanumeric() {
    for(int i = 0; i < reps; i++)
        isAlphanumeric("ab4r3rgf"+i);
}

public void testIsAlphanumeric2() {
    for(int i = 0; i < reps; i++)
        isAlphanumeric2("ab4r3rgf"+i);
}

    public boolean isAlphanumeric(String str) {
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if (!Character.isLetterOrDigit(c))
                return false;
        }

        return true;
    }

    public boolean isAlphanumeric2(String str) {
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a)
                return false;
        }
        return true;
    }

}
🌐
Delft Stack
delftstack.com › home › howto › java › check if character is alphanumeric java
How to Check if a Character Is Alphanumeric in Java | Delft Stack
February 24, 2025 - In Java, regular expressions, often referred to as regex or regexp, are sequences of characters that form a search pattern. This can also be used to determine whether a character is alphanumeric (a letter or a digit).
🌐
IncludeHelp
includehelp.com › java-programs › check-a-given-character-is-alphanumeric-or-not-without-using-a-built-in-method.aspx
Java program to check a given character is alphanumeric or not without using a built-in method
February 24, 2022 - In the above program, we imported ... methods main() and isAlphaNumeric(). The isAlphaNumeric() method returns a Boolean value based on input character, it returns true if the given character is alphanumeric otherwise it returns false....
🌐
Techie Delight
techiedelight.com › home › java › check if a string contains alphanumeric characters in java
Check if a String contains alphanumeric characters in Java | Techie Delight
3 weeks ago - 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. ... If the regular expression is frequently called, you might want to compile the regular expression for performance boost: ... We can use the Apache Commons Lang library, a method called isAlphanumeric() included in the StringUtils class. ... In plain Java, we can iterate over the string characters and check each character to be alphanumeric using Character.isLetterOrDigit(char).
Find elsewhere
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2015 › 12 › how-to-check-string-is-alphanumeric-or.html
JavaMadeSoEasy.com (JMSE): How to check string is alphanumeric or not in Java
In this post we will learn how to check string is alphanumeric or not using regex (regular expression) and apache commons StringUtils class in java.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-check-string-is-alphanumeric-or-not-using-regular-expression
How to check string is alphanumeric or not using Regular Expression - GeeksforGeeks
July 12, 2025 - const regex pattern("^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$"); // If the string // is empty return false if (str.empty()) { return false; } // Return true if the String // matched the ReGex if (regex_match(str, pattern)) { return true;; } else { return false; } } // Driver Code int main() { // Test Case 1: string str1= "GeeksforGeeks123"; cout <<isAlphaNumeric(str1) << endl; // Test Case 2: string str2= "GeeksforGeeks"; cout << isAlphaNumeric(str2) << endl; // Test Case 3: string str3= "GeeksforGeeks123@#"; cout << isAlphaNumeric(str3) << endl; // Test Case 4: string str4= "123"; cout << isAlphaNumeric(str4) << endl; //Test Case 5: string str5="@#"; cout << isAlphaNumeric(str5) << endl; return 0; } //This Code is contributed by Rahul Chauhan · Java · // Java program to check string is // alphanumeric or not using Regular Expression.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
String Contains Non-Alphanumeric Characters in Java - Java Code Geeks
July 14, 2023 - In this guide, we will discuss how to check if a string contains non-alphanumeric characters in Java. This can be useful in various scenarios, such as input validation or filtering out unwanted characters from a string.
🌐
Mkyong
mkyong.com › home › regular expressions › java regex check alphanumeric string
Java regex check alphanumeric string - Mkyong.com
November 8, 2020 - Below is a Java regex to match alphanumeric characters. ... package com.mkyong.regex.string; public class StringUtils { private static final String ALPHANUMERIC_PATTERN = "^[a-zA-Z0-9]+$"; public static boolean isAlphanumeric(final String input) { return input.matches(ALPHANUMERIC_PATTERN); } }
🌐
Baeldung
baeldung.com › home › java › java string › check if a string contains non-alphanumeric characters
Check if a String Contains Non-Alphanumeric Characters | Baeldung
January 8, 2024 - Here comes the least flexible of all the techniques used so far. The method isAlphanumeric() in StringUtils supports all the Unicode letters or digits, but there’s no support for identifying the language script used in the string.
🌐
javaspring
javaspring.net › blog › isalphanumeric-java
Mastering `isalphanumeric` in Java — javaspring.net
An alphanumeric character is either a letter (A - Z, a - z) or a digit (0 - 9). Java provides multiple ways to perform this check, and understanding these methods is crucial for validating user input, processing data, and ensuring the integrity ...
🌐
The Crazy Programmer
thecrazyprogrammer.com › home › how to check string is alphanumeric in java
How to Check String is Alphanumeric in Java
July 22, 2015 - 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.
🌐
How to do in Java
howtodoinjava.com › home › java regular expressions › regex for alphanumeric characters (+ example)
Regex for Alphanumeric Characters (+ Example)
June 29, 2024 - Use regex “\\p{Alnum}+” to match any alphanumeric character (letters and digits) recognized in any Java Locale.
🌐
Educative
educative.io › answers › how-to-check-if-a-character-is-alphanumeric-in-java-with-if-else
How to check if a character is alphanumeric in Java with if-else
An alphanumeric character is a character that is either a number or an alphabet. The logic to find out if a character is alphanumeric is listed below: · We can see if a character is an alphanumeric character ...
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - Thus, we can differentiate between alphanumeric and non-alphanumeric characters by their ASCII values. In this method, we’ll make an empty string object, traverse our input string and fetch the ASCII value of each character.
🌐
TutorialsPoint
tutorialspoint.com › program-to-find-whether-a-string-is-alphanumeric
Program to find whether a string is alphanumeric.
November 21, 2019 - Any word that contains numbers and letters is known as alphanumeric. The following regular expression matches the combination of numbers and letters. The matches method of the String class accepts a regular expression (in the form of a String) and
🌐
javaspring
javaspring.net › blog › alphanumeric-in-java
Mastering Alphanumeric Handling in Java — javaspring.net
Alphanumeric characters include: Letters: Both uppercase (A - Z) and lowercase (a - z). Digits: The numbers from 0 - 9. The Character class in Java provides useful methods to check if a given character is a letter, digit, or alphanumeric.