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)