How would you check if a String was a number before parsing it?
You can also use StringUtils.isNumericSpace
which returns true
for empty strings and ignores internal spaces in the string. Another way is to use NumberUtils.isParsable
which basically checks the number is parsable according to Java. (The linked javadocs contain detailed examples for each method.)
The primary goal - in my mind - to detect one (or more) non numeric character. If there is minimal one then the string cannot be number. The regex would be a perfect possibility for this... The '\D' means in regex one non numeric character. (I am thinking about integers only at the moment, the doubles are an another question.)
It should work in my theory:
but it doesn't work in real llife cuz the result is "false"...
Similar in Perl is perfect:
Result: 1