1. Using Regular expressions
First, import Java.util.regex.Pattern and Java.util.regex.Matcher
/* Method 1): recommended, fastest * use regular expressions to determine whether a string is a number * @param str an incoming string * @return is an integer returns True, otherwise returns false */ public boolean IsNumeric (STRING STR) { pattern Pattern = pattern.compile ("[0-9]*"); matcher isnum = pattern.matcher (str); if ( !isNum.matches () ) { return false; } return true; } /*&nbsP; 2) Recommended, fastest * determine if integer * @param str incoming string * @return is an integer returns True, otherwise returns false */ public static boolean Isinteger (STRING STR) { Pattern Pattern = pattern.compile ("^[-\\+]?[ \\d]*$ "); return pattern.matcher (str). Matches ( ); }
2. Functions with Java
public static Boolean isnumeric (String str) {for (int i = 0; i < str.length (); i++) {System.out.println (Str.cha RAt (i)); if (! Character.isdigit (Str.charat (i))) {return false; }} return true;
3. Using Org.apache.commons.lang
org.apache.commons.lang.stringutils;boolean isnunicodedigits=stringutils.isnumeric ("aaa123456789"); Http://jakarta.apache.org/commons/lang/api-release/index.html the explanations below:public static boolean IsNumeric (STRING&NBSP;STR) checks if the string contains only unicode digits . a decimal point is not a unicode digit and returns false.null will return false. an empty string ("") will return true. Stringutils.isnumeric (NULL) = falsestringutils.isnumeric ("") = truestringutils.isnumeric (" ") = falsestringutils.isnumeric ("123") = truestringutils.isnumeric ("12 3") = falsestringutils.isnumeric ("AB2C") = falsestringutils.isnumeric ("12-3") = falsestringutils.isnumeric ("12.3") = false
4. Judging ASCII code value
IsNumeric0 (String str) {(i=str.length ();--i>=0;) {Chr=str.charat (i); (chr<48 | | chr>57); } ; }
5. Determine whether the characters in Str are 0-9
ISNUMERIC3 (String str) {string number = "0123456789"; (i = 0;i < number.length; i + +) {(Number.indexof (Str.charat (i)) = =-1) {; } } ;}
6. Capturing NumberFormatException anomalies
ISNUMERIC00 (String str) {{integer.parseint (str); ; } (NumberFormatException e) {System.out.println ("Exception: \" "+ str +" \ "is not a number/integer ..."); ; }}
This article is from the "Exist" blog, make sure to keep this source http://exist.blog.51cto.com/10305882/1949627
Java determines if a string variable is a number