Implement a function to determine whether a string represents a numeric value (including integers and decimals). For example, the string "+100", "5e2", "123", "3.1416" and " -1E-16" all represent numeric values. But "12e", "1a3.14", "1.2.3", "+-5" and "12e+4.3" are not.
1 Public classDemo2 {2 3 //array subscript member variable4 intindex; 5 6 Public BooleanIsnumeric_2 (Char[] str) { 7 //Input Exception8 if(str = =NULL) 9 return false; Tenindex = 0; One //start with a positive sign A if(Str[index] = = ' + ' | | str[index] = = '-') -index++; - if(Index = =str.length) the return false; - //set numeric to determine whether a number - Booleannumeric =true; - scandigits (str); + if(Index! =str.length) { - //decimal + if(Str[index] = = '. ') { Aindex++; at scandigits (str); - if(Index < Str.length && (str[index] = = ' E ' | | str[index] = = ' E ')) -numeric =isexponential (str); -}Else if(Str[index] = = ' E ' | | str[index] = = ' E ') -numeric =isexponential (str); - Else in //An exception character has occurred -numeric =false; to } + - returnNumeric && Index = =str.length; the } * $ //Scan an array, if the current character is a number, index++Panax Notoginseng Private voidScandigits (Char[] str) { - while(Index < str.length && Str[index] >= ' 0 ' && Str[index] <= ' 9 ') theindex++; + } A the //determines whether the end of a numeric value represented by scientific notation + Private BooleanIsexponential (Char[] str) { - if(Str[index]! = ' E ' && str[index]! = ' E ') $ return false; $index++; - if(Index = =str.length) - return false; the if(Str[index] = = ' + ' | | str[index] = = '-') -index++; Wuyi if(Index = =str.length) the return false; - scandigits (str); Wu //If there are special characters, index will not be str.length - returnindex = = str.length?true:false; About } $}
Reprinted from: http://blog.csdn.net/zjkc050818/article/details/72818475
A string representing a numeric value (Java implementation)