Re: test if a string is an integer?
From: Raymond DeCampo (rdecampo_at_spam.twcny.spam.rr.spam.com.spam)
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();
> 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. :)
-- XML is the programmer's duct tape.