It is recommended to use the second method, the fastest one.
Method One: Use Java's own function
Java code
- Public Static boolean isnumeric (String str) {
- For (int i = Str.length ();--i>=0;) {
- if (! Character.isdigit (Str.charat (i))) {
- return false;
- }
- }
- return true;
- }
public static Boolean isnumeric (String str) {for (int i = Str.length ();--i>=0;) { if (! Character.isdigit (Str.charat (i))) { return false; } } return true;}
Method Two:
Java code
- /*
- * Determines whether it is an 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 ();
- }
/* * Determine if integer * @param str passed in string * @return is integer returns TRUE, otherwise false * /public static boolean Isinteger (string STR) { Pattern pattern = pattern.compile ("^[-\\+]?[ \\d]*$ "); return Pattern.matcher (str). matches (); }
Method Three:
Java code
- Public Static boolean isnumeric (String str) {
- Pattern pattern = Pattern.compile ("[0-9]*");
- return Pattern.matcher (str). matches ();
- }
public static Boolean isnumeric (String str) { Pattern pattern = Pattern.compile ("[0-9]*"); return Pattern.matcher (str). matches (); }
Method Four:
Java code
- public final Static boolean isnumeric (string s) {
- if (s ! = null && !
- return s.matches (
- else
- return false;
- }
Public final Static Boolean isnumeric (String s) {if (s! = null &&! "). Equals (S.trim ())) return s.matches ("^[0-9]*$"); Elsereturn false;}
3 method Five: using ASCII code
Java code
- Public Static boolean isnumeric (String str) {
- For (int i=str.length ();--i>=0;) {
- int Chr=str.charat (i);
- if (chr<| | chr>)
- return false;
- }
- return true;
- }
public static Boolean isnumeric (String str) {for (int i=str.length ();--i>=0;) { int chr=str.charat (i); if (chr<48 | | chr>57) return false; }
Article Address:
http://javapub.iteye.com/blog/666544
Five ways to judge whether a string is a number in Java (GO)