Re: test if a string is an integer?
From: Raymond DeCampo (rdecampo_at_spam.twcny.spam.rr.spam.com.spam)
Date: 07/16/04
- Previous message: dave: "OO design in servlet design question"
- In reply to: Chris Dutton: "Re: test if a string is an integer?"
- Next in thread: lordy: "Re: test if a string is an integer?"
- Reply: lordy: "Re: test if a string is an integer?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 16 Jul 2004 12:23:52 GMT
Chris Dutton wrote:
> As an alternative to the solution already provided...
>
> import java.util.regex.*;
>
> ...
>
> Pattern integerPattern = Pattern.compile("^\d*$");
> Matcher matchesInteger = integerPattern.matcher(myString);
> boolean isInteger = matchesInteger.matches();
>
> or:
>
> boolean isInteger = Pattern.matches("^\d*$", myString);
Note that this method is *not* equivalent to the other methods which use
Integer.parseInt(). This method will accept inputs that are outside the
range of a Java int, while Integer.parseInt() will not. Use whichever
one is appropriate for your application.
Also, I believe you forgot about the negative sign. :)
HTH,
Ray
-- XML is the programmer's duct tape.
- Previous message: dave: "OO design in servlet design question"
- In reply to: Chris Dutton: "Re: test if a string is an integer?"
- Next in thread: lordy: "Re: test if a string is an integer?"
- Reply: lordy: "Re: test if a string is an integer?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|