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
🌐
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));
🌐
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.
🌐
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 ?
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;
    }

}
🌐
Onenetwork
devnet.onenetwork.com › oms › apps › DeveloperNetwork › www › docs › api › javadoc › com › onenetwork › platform › tools › util › StringUtils.html
StringUtils (ONE Platform - Server API)
public static boolean isAlphaNumeric · (char ch) Check Character is alphanumeric · Parameters: ch - Returns: true iff char is Number of Character else false · public static boolean isAlphaNumeric · (String str) public static String requireNonNullOrBlank ·
🌐
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...
🌐
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 ·
🌐
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); } }
Find elsewhere
🌐
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]+")); } }
🌐
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 ·
🌐
Java2s
java2s.com › example › java-api › org › apache › commons › lang › stringutils › isalphanumeric-1-2.html
Example usage for org.apache.commons.lang StringUtils isAlphanumeric
Please remove all other characters"); } try { Server server = application.getServers().get(0); application.getAliases().add(alias); hipacheRedisUtils.writeNewAlias(alias, application, server.getServerAction().getServerPort()); applicationDAO.save(application); } catch (DataAccessException e) { throw new ServiceException(e.getLocalizedMessage(), e); } } From source file:com.att.aro.ui.view.menu.tools.RegexWizard.java · private String formatSpecialCharacters(String text) { boolean hasSpecialChars = !StringUtils.isAlphanumeric(text); StringBuffer sb = new StringBuffer(); if (hasSpecialChars) { Pattern pattern = Pattern.compile("[^a-zA-Z0-9]"); for (int i = 0; i < text.length(); i++) { Matcher match = pattern.matcher(text.substring(i, i + 1)); if (match.find()) { // is a special character sb.append("\\" + text.charAt(i)); } else { sb.append(text.charAt(i)); }/*from ww w .
🌐
javaspring
javaspring.net › blog › isalphanumeric-java
Mastering `isalphanumeric` in Java — javaspring.net
You can iterate over each character in a string to check if it is alphanumeric: public class AlphanumericCharacterClass { public static boolean isAlphanumeric(String str) { for (char c : str.toCharArray()) { if (!Character.isLetterOrDigit(c)) ...
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › program-to-find-whether-a-string-is-alphanumeric
Program to find whether a string is alphanumeric.
November 21, 2019 - import java.util.Scanner; public class AlphanumericString { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter input string: "); String input = sc.next(); String regex = "^[a-zA-Z0-9]+$"; boolean result = input.matches(regex); if(result) { System.out.println("Given string is alpha numeric"); } else { System.out.println("Given string is not alpha numeric"); } } }
🌐
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.
🌐
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.
🌐
javaspring
javaspring.net › blog › alphanumeric-in-java
Mastering Alphanumeric Handling in Java — javaspring.net
import java.util.regex.Pattern; public class RegexAlphanumericCheck { public static boolean isAlphanumericRegex(String str) { String pattern = "^[a-zA-Z0-9]+$"; return Pattern.matches(pattern, str); } public static void main(String[] args) { String test1 = "Abc123"; String test2 = "Abc@123"; System.out.println(isAlphanumericRegex(test1)); // true System.out.println(isAlphanumericRegex(test2)); // false } } Sometimes, we need to generate alphanumeric strings, for example, for creating unique identifiers.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › character_isalphabetic.htm
Java - Character isAlphabetic() Method
The Java Character isAlphabetic() method accepts a valid Unicode code point as an argument, and checks whether the corresponding code point character is an alphabet or not. A character is said to be Alphabetic if its general category type specified