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
🌐
MuleSoft
docs.mulesoft.com › dataweave › latest › dw-strings-functions-isalphanumeric
isAlphanumeric | MuleSoft Documentation
%dw 2.0 import isAlphanumeric from dw::core::Strings output application/json --- { "a": isAlphanumeric(null), "b": isAlphanumeric(""), "c": isAlphanumeric(" "), "d": isAlphanumeric("abc"), "e": isAlphanumeric("ab c"), "f": isAlphanumeric("ab2c"), "g": isAlphanumeric("ab-c") } { "a": false, "b": false, "c": false, "d": true, "e": false, "f": true, "g": false } Helper function that enables isAlphanumeric to work with a null value.
🌐
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.
🌐
Educative
educative.io › answers › what-is-stringutilsisalphanumeric-in-java
What is StringUtils.isAlphanumeric in Java?
System.out.printf("The output of StringUtils.isAlphanumeric() for the string - '%s' is %s", s, StringUtils.isAlphanumeric(s));
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;
    }

}
🌐
Mkyong
mkyong.com › home › regular expressions › java regex check alphanumeric string
Java regex check alphanumeric string - Mkyong.com
November 8, 2020 - StringUtils.java · 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); } } Below is a simple JUnit 5 tests to validate the alphanumeric characters.
🌐
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 - // Java program to check a given character is // alphanumeric or not without using // the built-in method import java.util.Scanner; public class Main { static boolean isAlphaNumeric(char ch) { if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true; return false; } public static void main(String[] args) { Scanner X = new Scanner(System.in); char ch = 0; System.out.printf("Enter character: "); ch = X.next().charAt(0); if (isAlphaNumeric(ch)) System.out.printf("Given character is an alphanumeric character\n"); else System.out.printf("Given character is not an alphanumeric character\n"); } } RUN 1: Enter character: K Given character is an alphanumeric character RUN 2: Enter character: 8 Given character is an alphanumeric character RUN 3: Enter character: % Given character is not an alphanumeric character ·
🌐
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
Example 2 to check string is alphanumeric or not in java using org.apache.commons.lang.StringUtils.isAlphanumeric(str) > Jar used in above program = commons-lang.jar · Labels: Core Java RegEx · Newer Post Older Post Home · eEdit · Must read for you : Algorithm ·
Find elsewhere
🌐
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 - 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.isLette...
🌐
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 - import java.util.regex.*; public class CheckCharAlpha { public static void main(String[] args) { char a = '4'; boolean isAlphanumeric = String.valueOf(a).matches("[a-zA-Z0-9]"); System.out.println("Is the character alphanumeric? " + isAlphanumeric); } } Is the character alphanumeric ?
🌐
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.
🌐
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 - java.util.regex.*; class AlphanumericExample { public static void main(String...s) { String s1="adA12", s2="jh@l"; System.out.println(s1.matches("[a-zA-Z0-9]+")); System.out.println(s2.matches("[a-zA-Z0-9]+")); } }
🌐
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
Without any inbuilt method, we can check if a given character is alphanumeric or not. 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 ...
🌐
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 - Match the given string with the regex, in Java, this can be done by using Pattern.matcher() Return true if the string matches with the given regex, else return false · Below is the implementation of the above approach. C++ // C++ program to check // String is alphanumeric or not using Regular Expression #include <iostream> #include <regex> using namespace std; // Function to validate the // String is alphanumeric or not using Regular Expression bool isAlphaNumeric(string str) { // Regex to check valid alphanumeric String.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Check if String Contains Only Letters & Numbers - Java Code Geeks
February 27, 2024 - This Java program defines a method isAlphaNumeric that checks if the given string contains only alphanumeric characters using a regular expression [a-zA-Z0-9]+. You can call this method with any string, and it will return true if the string ...
🌐
Syncloop
syncloop.com › docs › string-isalphanumeric.html
isAlphanumeric | Syncloop Documentation
java · applyGraphQLSchema · getEntireSchema · getGraphQLSchema · introspectionQuery · io · bytesToFile · bytesToStream · close · createAsEmptyByteArray · mark · markSupported · read · readAsString · readerToString · replace · reset · skip · streamToBytes · streamToReader · streamToString · stringToInputStream · stringToReader · json ·
🌐
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.