如何检查String中的String是否为数字 [英] How to check if a String is numeric in Java

查看:179
本文介绍了如何检查String中的String是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在解析之前检查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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆