PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2015-10-24 09:17:00  修改时间:2024-11-21 15:59:57  分类:07.Java基础  编辑
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!-- Apache Commons:Lang3 -->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

 

Apache Commons:Lang3 包下,有对 数字字符串 的快捷操作方法:

public static void main(String[] args) {
    // 判断字符串是否为 数字
    //【注意】NumberUtils.isNumber() 已废弃
    System.out.println(NumberUtils.isNumber("5.96"));               // 结果是true
    System.out.println(NumberUtils.isNumber("0000000000596"));      // 结果是true
    System.out.println(NumberUtils.isNumber("s5"));                 // 结果是false
    System.out.println(NumberUtils.isNumber("12 3"));               // 结果是false
    System.out.println("++++++++++++++++++++++++++++");
    System.out.println(NumberUtils.isCreatable("5.96"));           // 结果是true
    System.out.println(NumberUtils.isCreatable("-5.96"));          // 结果是true
    System.out.println(NumberUtils.isCreatable("0000000000596"));  // 结果是true
    System.out.println(NumberUtils.isCreatable("s5"));             // 结果是false
    System.out.println(NumberUtils.isCreatable("12 3"));           // 结果是false

    System.out.println("++++++++++++++++++++++++++++");
    System.out.println("++++++++++++++++++++++++++++");

    // 判断字符串是否为 全数字
    //【注意】数字字符串相关的操作,建议统一用 NumberUtils 类下的方法,即不用 StringUtils.isNumeric()
    System.out.println(NumberUtils.isDigits("-5.96"));              // 结果是false
    System.out.println(NumberUtils.isDigits("0000000000.596"));     // 结果为false
    System.out.println(NumberUtils.isDigits("0000000000596"));      // 结果为true
    System.out.println(StringUtils.isNumeric("-5.96"));             // 结果为false    
    System.out.println(StringUtils.isNumeric("0000000000.596"));    // 结果为false
    System.out.println(StringUtils.isNumeric("0000000000596"));     // 结果为true
}