Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:03:50+00:00 2026-06-12T17:03:50+00:00

What is the fastest way to check that a String contains only alphanumeric characters.

  • 0

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 information about how to improve the performance of the alphanumeric checking process. I am wondering if there is a more efficient way to check than using pre-compiled regular expressions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:03:51+00:00Added an answer on June 12, 2026 at 5:03 pm

    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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the simplest and fastest way to check if string is single URL
I'm looking for the fastest way to check that entry exists... All my life,
I am trying to find the fastest way to check whether a given number
I am looking to the fastest and the correct way to check if a
What is the fastest way to compare a string with an array of strings
I'm wondering what is the fastest way that I can write some code. I
I have an app that fetches data from the internet and uses CoreData to
What is the best/fastest way to merge two xml documents with ruby? I have
Suppose that you want to check what input string a user has entered in
I am trying to figure out the fastest way (in PHP 5) to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.