How to test if a string only contains a number

Hi,What is the simplest way to test if a string only contains a number?For example "324" only contains a number while "abc" not.Thanks.Pengyou
[177 byte] By [pengyoua] at [2007-11-27 7:49:00]
# 1
try to parse it with Integer.parseInt(String str)if it throws a "numberformatexception" string does not contain only digits ; if not, it contains only digitsor use a regex: the string has to match "[0-9]*" , or something like that :)
calvino_inda at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 2
> try to parse it with Integer.parseInt(String str)> > if it throws a "numberformatexception" string does> not contain only digits ; if not, it contains only> digits-123 ?
sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 3
sabre> it's true that parseInt won't take into considerations all kind of number, that's why i also suggested regex but for "basic" numbers (natural positives int), i guess parseInt is enough :)
calvino_inda at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 4
> sabre> it's true that parseInt won't take into> considerations all kind of number, that's why i also> suggested regex You edited your post after I quoted it!
sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 5
Or you can just test each character with Character.isDigit();
malcolmmca at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 6
> Or you can just test each character with> Character.isDigit();Obviously a DIY enthusiast.
sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 7
It's efficiency - I can't bear to think about the overheads of using regexp uneccesarilly.
malcolmmca at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 8
> It's efficiency - I can't bear to think about the> overheads of using regexp uneccesarilly.I second that, although I weaken my own standards by using regex more and more.
quittea at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 9
> It's efficiency - I can't bear to think about the> overheads of using regexp uneccesarilly.Have you measured the overhead when using regex in this case?
sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 10

> Have you measured the overhead when using regex in

> this case?

Can't be bothered - but the most efficient way regex could work on it would be to check the value range of each char, once. Which is what the "manual" solution does as well. Now considering that patterns need to be compiled before used, this compilation time can be considered overhead, although, admittedly, it will hardly matter if it's a one-time action on user input.

Which solution would be the best one IMO strongly depends on the allowed value range and the further use of that String, which the OP never specified.

CeciNEstPasUnProgrammeura at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 11

> > sabre> it's true that parseInt won't take into

> > considerations all kind of number, that's why i

> also

> > suggested regex

>

> You edited your post after I quoted it!

actually i was editing it while you posted your answer, i think

i'm not sure it's possible to edit your own post once someone else has answered behind :)

calvino_inda at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 12

> > > sabre> it's true that parseInt won't take into

> > > considerations all kind of number, that's why i

> > also

> > > suggested regex

> >

> > You edited your post after I quoted it!

>

> actually i was editing it while you posted your

> answer, i think

>

> i'm not sure it's possible to edit your own post once

> someone else has answered behind :)

I had 'quoted' it and was in the process of answering it!

sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 13

> > > > sabre> it's true that parseInt won't take into

> > > > considerations all kind of number, that's why

> i

> > > also

> > > > suggested regex

> > >

> > > You edited your post after I quoted it!

> >

> > actually i was editing it while you posted your

> > answer, i think

> >

> > i'm not sure it's possible to edit your own post

> once

> > someone else has answered behind :)

>

> I had 'quoted' it and was in the process of answering

> it!

no harm done ^^

by the way , looks like OP found what he needed since he isn't saying anything

calvino_inda at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 14

> > Have you measured the overhead when using regex in

> > this case?

>

> Can't be bothered - but the most efficient way regex

> could work on it would be to check the value range of

> each char, once. Which is what the "manual" solution

> does as well. Now considering that patterns need to

> be compiled before used, this compilation time can be

> considered overhead, although, admittedly, it will

> hardly matter if it's a one-time action on user

> input.

>

> Which solution would be the best one IMO strongly

> depends on the allowed value range and the further

> use of that String, which the OP never specified.

I have just compared the two approaches. On my average machine using JDK1.6 using test code

public static void testUsingRegex() throws Exception

{

java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\d+").matcher("0");

String[] targets = {"1234","12sx"};

long start = 0;

for (int i = -1000; i < NUMBER_OF_ITERATIONS; i++)

{

if (i == 0)

start = System.currentTimeMillis();

for (String target : targets)

{

m.reset(target);

boolean valid = m.matches();

}

}

long end = System.currentTimeMillis();

double delta = 1000.0 * (end-start) / NUMBER_OF_ITERATIONS;

System.out.println("Regex " + delta + " us");

}

public static void testUsingCharArray() throws Exception

{

java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\d+").matcher("0");

String[] targets = {"1234","12sx"};

long start = 0;

for (int i = -1000; i < NUMBER_OF_ITERATIONS; i++)

{

if (i == 0)

start = System.currentTimeMillis();

for (String target : targets)

{

boolean valid = true;

char[] chars = target.toCharArray();

for (char ch : chars)

{

if (!Character.isDigit(ch))

{

valid = false;

break;

}

}

}

}

long end = System.currentTimeMillis();

double delta = 1000.0 * (end-start) / NUMBER_OF_ITERATIONS;

System.out.println("Char array " + delta + " us");

}

I get as result

Regex 0.44248 us

Char array 0.25672 us

averaged over 100,000,000 iterations.

So yes, as expected, converting to a char array is faster but considering the actual values and the simplicity of the regex code I know which I prefer.

Message was edited by:

sabre150

sabre150a at 2007-7-12 19:29:53 > top of Java-index,Java Essentials,Java Programming...
# 15

Go the regex, it's more flexible... but, I'm working on a system with 21 million features... and trust me, such micro-optimisations can really add up... so it's horses for courses... the hard bit I've found is indentifying the bottlenecks not (usually) resolving them.

total time on 21,000,000

Regex 14516

Manual 2750

It's also interesting to see what happens when you make the target strings progressively longer... regex doesn't slow down (proportionatly) as much the char array (I wonder why?)... but it's still 14 seconds faster.

Regex 17485

Manual 3359

I'd use a regex unless/until I had proof that it's too slow. Performance shouldn't ever be the primary design consideration, but it should always be a secondary one.

Keith.

corlettka at 2007-7-21 22:21:59 > top of Java-index,Java Essentials,Java Programming...
# 16

'corlettk' - you need to show how you use the regex to get your timings because they do not stack up with my results.

The only way I can get a timing ratio comparable with yours is if I use boolean valid = target.matches("\\d+");

which is definitely not the way to use a regex when you want to compare performance.

Message was edited by:

sabre150

sabre150a at 2007-7-21 22:21:59 > top of Java-index,Java Essentials,Java Programming...
# 17

> > try to parse it with Integer.parseInt(String str)

> >

> > if it throws a "numberformatexception" string does

> > not contain only digits ; if not, it contains only

> > digits

>

> -123 ?

It wasn't clear to me whether the OP means "only contains digits" or "contains a valid number" or "contains a valid integer".

jverda at 2007-7-21 22:21:59 > top of Java-index,Java Essentials,Java Programming...