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 347151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:14:38+00:00 2026-05-12T11:14:38+00:00

In my limited experience, I’ve been on several projects that have had some sort

  • 0

In my limited experience, I’ve been on several projects that have had some sort of string utility class with methods to determine if a given string is a number. The idea has always been the same, however, the implementation has been different. Some surround a parse attempt with try/catch

public boolean isInteger(String str) {
    try {
        Integer.parseInt(str);
        return true;
    } catch (NumberFormatException nfe) {}
    return false;
}

and others match with regex

public boolean isInteger(String str) {
    return str.matches("^-?[0-9]+(\\.[0-9]+)?$");
}

Is one of these methods better than the other? I personally prefer using the regex approach, as it’s concise, but will it perform on par if called while iterating over, say, a list of a several hundred thousand strings?

Note: As I’m kinda new to the site I don’t fully understand this Community Wiki business, so if this belongs there let me know, and I’ll gladly move it.

EDIT:
With all the TryParse suggestions I ported Asaph’s benchmark code (thanks for a great post!) to C# and added a TryParse method. And as it seems, the TryParse wins hands down. However, the try catch approach took a crazy amount of time. To the point of me thinking I did something wrong! I also updated regex to handle negatives and decimal points.

Results for updated, C# benchmark code:

00:00:51.7390000 for isIntegerParseInt
00:00:03.9110000 for isIntegerRegex
00:00:00.3500000 for isIntegerTryParse

Using:

static bool isIntegerParseInt(string str) {
    try {
        int.Parse(str);
        return true;
    } catch (FormatException e){}
    return false;
}

static bool isIntegerRegex(string str) {
    return Regex.Match(str, "^-?[0-9]+(\\.[0-9]+)?$").Success;
}

static bool isIntegerTryParse(string str) {
    int bob;
    return Int32.TryParse(str, out bob);
}
  • 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-05-12T11:14:38+00:00Added an answer on May 12, 2026 at 11:14 am

    I just ran some benchmarks on the performance of these 2 methods (On Macbook Pro OSX Leopard Java 6). ParseInt is faster. Here is the output:

    This operation took 1562 ms.
    This operation took 2251 ms.
    

    And here is my benchmark code:

    
    public class IsIntegerPerformanceTest {
    
        public static boolean isIntegerParseInt(String str) {
            try {
                Integer.parseInt(str);
                return true;
            } catch (NumberFormatException nfe) {}
            return false;
        }
    
        public static boolean isIntegerRegex(String str) {
            return str.matches("^[0-9]+$");
        }
    
        public static void main(String[] args) {
            long starttime, endtime;
            int iterations = 1000000;
            starttime = System.currentTimeMillis();
            for (int i=0; i<iterations; i++) {
                isIntegerParseInt("123");
                isIntegerParseInt("not an int");
                isIntegerParseInt("-321");
            }
            endtime = System.currentTimeMillis();
            System.out.println("This operation took " + (endtime - starttime) + " ms.");
            starttime = System.currentTimeMillis();
            for (int i=0; i<iterations; i++) {
                isIntegerRegex("123");
                isIntegerRegex("not an int");
                isIntegerRegex("-321");
            }
            endtime = System.currentTimeMillis();
            System.out.println("This operation took " + (endtime - starttime) + " ms.");
        }
    }
    

    Also, note that your regex will reject negative numbers and the parseInt method will accept them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a web-developer working in PHP. I have some limited experience with using
My PHP experience is rather limited. I've just inherited some stuff that looks odd
I have a very limited experience of database programming and my applications that access
I have some experience with unit testing and mocks. In my limited experience I
I have limited experience of using PHP, but having done some searching around it
I have limited programming experience, and almost none with PHP. However I've been given
I'm a developer with limited HTML/CSS design experience. I have been stuck trying to
I have limited experience with .net. My app throws an error this.dateTimeFormat is undefined
My experience with application servers is limited to some basic servlet coding, so I
I have a very limited experience with Firefox extensions development (read XUL School ,

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.