• java:判断字符串是否为数字


    java:判断字符串是否为数字

    1 简单封装,兼容负数情况(不含小数点)

    package com.xiaoxu.utils.str;
    
    import org.springframework.util.StringUtils;
    
    /**
     * @author xiaoxu
     * @date 2022-08-03
     * spring_boot:com.xiaoxu.utils.str.StrUtil
     */
    public class StrUtil {
        /*
        * 判断字符串是否为数字(不能含小数点)
        * */
        public static boolean isNumeric(String str){
            if(!StringUtils.hasLength(str)){
                return false;
            }
            CharSequence cs = str;
            if(cs.charAt(0)=='-'&&StringUtils.countOccurrencesOf(str,"-")==1){
                cs = str.substring(1);
            }
            for(int var1 = 0;var1<cs.length();++var1){
                char c = cs.charAt(var1);
                if(!Character.isDigit(c)){
                    return false;
                }
            }
            try {
                Long.parseLong(cs.toString());
            } catch (NumberFormatException e) {
                return false;
            }
            return true;
        }
    
        public static void main(String[] args) {
            System.out.println(isNumeric("-1 "));
            System.out.println(isNumeric(" -1"));
            System.out.println(isNumeric("-1"));
            System.out.println(isNumeric("a1212"));
            System.out.println(isNumeric("1213"));
            System.out.println(isNumeric("000071213"));
            System.out.println(isNumeric("-00007100213"));
            System.out.println(isNumeric("--00007100213"));
            System.out.println(isNumeric("-00007100213小徐"));
            System.out.println(isNumeric("-00007100213.0"));
            System.out.println(isNumeric("00"));
            System.out.println(isNumeric("-00"));
            System.out.println(isNumeric("0"));
            System.out.println(isNumeric("-0"));
            System.out.println(isNumeric("-"));
            System.out.println(isNumeric("- "));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    2 执行结果

    false
    false
    true
    false
    true
    true
    true
    false
    false
    false
    true
    true
    true
    true
    false
    false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    ClickHouse 物化视图
    pytest+requests实现自动化代码编写思路
    大数据杂谈
    如何在迅睿CMS中使用if语句判断多个栏目ID
    GPU如何统计显存占用
    Spring框架技术的核心与设计思想
    Python随手记
    微服务架构必备技术栈:万变不离其宗的奥义!
    决策树分析及其在项目管理中的应用
    有什么docker容器可以监视本地请求的码
  • 原文地址:https://blog.csdn.net/a232884c/article/details/126136390