如何在Java中检查字符串是否为数字

编程入门 行业动态 更新时间:2024-10-21 10:00:36
本文介绍了如何在Java中检查字符串是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在解析字符串之前如何检查字符串是否为数字?

How would you check if a String was a number before parsing it?

推荐答案

With 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 并忽略字符串中的内部空格.另一种方法是使用 NumberUtils.isParsable 基本上检查数字是否可根据 Java 解析.(链接的 javadocs 包含每种方法的详细示例.)

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.)

更多推荐

如何在Java中检查字符串是否为数字

本文发布于:2023-10-14 22:18:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数字   如何在   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!