(RPM) Java six ways to determine if a string variable is a number

Source: Internet
Author: User

1. Using Regular Expressionsfirst, import Java.util.regex.Pattern and Java.util.regex.Matcher
/*** Use regular expressions to determine if a string is a number *@paramSTR *@return     */     Public Booleanisnumeric (String str) {pattern pattern= Pattern.compile ("[0-9]*"); Matcher Isnum=Pattern.matcher (str); if( !isnum.matches ()) {               return false; }           return true; }

2. Functions with Java

 Public Static Boolean isnumeric (String str) {   for (int i = 0; i < str.length (); i++) {System.out.print    ln (Str.charat (i));  if (!    Character.isdigit (Str.charat (i))) {        returnfalse;   }}  returntrue;

3. Using Org.apache.commons.lang

org.apache.commons.lang.StringUtils;BooleanIsnunicodedigits=stringutils.isnumeric ("aaa123456789"); http://jakarta.apache.org/commons/lang/api-release/index.html The following explanation: Public Static BooleanIsNumeric (String str) ChecksifThe String contains only Unicode digits. A decimal point was not a Unicode digit and returnsfalse.NULLWouldreturn false. An empty String ("") wouldreturn 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

 Public Static Boolean isNumeric0 (String str)
{
   for (int i=str.length ();--i>=0;)
{   int chr=str.charat (i);    if (chr<48 | | chr>57)      return false ;
} returntrue;

5. Determine whether the characters in Str are 0-9

 Public Static Boolean isNumeric3 (String str)
{ final String number = "0123456789"; for (int i = 0; i < number.length; i + +)
{
if (Number.indexof (Str.charat (i)) = =-1)
{
returnfalse; } } return true ;}

6. Capturing NumberFormatException anomalies

 Public Static Boolean isNumeric00 (String str)
{ try{ integer.parseint (str);    return true ; } Catch (NumberFormatException e)
{System.out.println ("Exception: \" "+ str +" \ "is not a number/integer ..."); returnfalse; }}

(GO) Java to determine whether a string variable is a number of six methods summary

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.