不灭的焱

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

作者:php-note.com  发布于:2019-12-08 17:23  分类: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
}