Java determines if a string variable is a number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.