Ken Dad's Stringutils.isnumeric (String str)

Source: Internet
Author: User

A bug was encountered in the project and the result of debugging was Stringutils.isnumeric (String str) In the tricks (using org.apache.commons.lang.StringUtils), the following code is to determine that a parameter is non-null and is an integer:

if (Stringutils.isnumeric (str) && Stringutils.isnotblank (str)) {            // do sth}

In the simple code, but hide the bug!

because if str = "-1"; Stringutils.isnumeric (str) returns the false! It's not a good father.

Here is the test:

 Public Static void Main (string[] args) {        System.out.println (stringutils.isnumeric ("-1"));}

Run Result: false

Ken-dad, huh? Isn't it easy to implement regular expressions? How can this, read the following source:

 Public Static Booleanisnumeric (String str) {if(str = =NULL) {            return false; }        intSZ =str.length ();  for(inti = 0; I < sz; i++) {            if(Character.isdigit (Str.charat (i)) = =false) {                return false; }        }        return true; }

Continue to jump in:

 Public Static boolean isdigit (char  ch) {        return isdigit ((int) ch );}

Go on:

 Public Static BooleanIsDigit (intcodepoint) {        BooleanBdigit =false; if(codepoint >= min_code_point && codepoint <=Fast_path_max) {Bdigit=Characterdatalatin1.isdigit (codepoint); } Else {            intPlane =Getplane (codepoint); Switch(plane) { Case(0): Bdigit=Characterdata00.isdigit (codepoint);  Break;  Case(1): Bdigit=Characterdata01.isdigit (codepoint);  Break;  Case(2): Bdigit=Characterdata02.isdigit (codepoint);  Break;  Case(3)://Undefined             Case(4)://Undefined             Case(5)://Undefined             Case(6)://Undefined             Case(7)://Undefined             Case(8)://Undefined             Case(9)://Undefined             Case(10)://Undefined             Case(11)://Undefined             Case(12)://Undefined             Case(13)://UndefinedBdigit =Characterdataundefined.isdigit (codepoint);  Break;  Case(14): Bdigit=Characterdata0e.isdigit (codepoint);  Break;  Case(15)://Private Use             Case(16)://Private UseBdigit =Characterdataprivateuse.isdigit (codepoint);  Break; default:                //The argument ' s plane is invalid, and thus are an invalid codepoint//Bdigit remains false;                 Break; }        }        returnBdigit; }

Failure in the following step:

Static boolean isdigit (int  ch) {        int type = getType (CH);         return (Type = = character.decimal_digit_number);    }

In other words, his implementation completely did not take into account the-+ prefix problem, this is not a silly fork?

The following results are false:

 Public Static void Main (string[] args) {        System.out.println (stringutils.isnumeric ("-1"));        System.out.println (Stringutils.isnumeric ("+1"));

This is his way of commenting:

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") =falseparameters:str the String to checkNULLReturns:true ifonly contains digits, and is non-NULL

Can only contain Unicode numbers, +,-,. None of the three can be counted as Unicode numbers.

Ken Dad's Stringutils.isnumeric (String str)

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.