如何检查String中的String是否为数字 [英] How to check if a String is numeric in Java
问题描述
如何在解析之前检查String是否为数字?
How would you check if a String was a number before parsing it?
推荐答案
使用 Apache Commons Lang 3.5及以上: NumberUtils.isCreatable
或 StringUtils.isNumeric
。
With Apache Commons Lang 3.5 and above: NumberUtils.isCreatable
or StringUtils.isNumeric
.
Apache Commons Lang 3.4及以下: NumberUtils.isNumber
或 StringUtils .isNumeric
。
With Apache Commons Lang 3.4 and below: NumberUtils.isNumber
or StringUtils.isNumeric
.
您还可以使用 StringUtils.isNumericSpace
对于空字符串返回 true
并忽略字符串中的内部空格。 (链接的javadoc包含每个方法的详细示例。)
You can also use StringUtils.isNumericSpace
which returns true
for empty strings and ignores internal spaces in the string. (The linked javadocs contain detailed examples for each method.)
这篇关于如何检查String中的String是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!