• 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
  • 相关阅读:
    plspm模型报错问题
    LeetCode 567. 字符串的排列
    Linux系统下交叉编译工具的安装实现
    用DIV+CSS技术设计的网上书城网页与实现制作(大一Web课程设计)
    【DLY-310端子排型电流继电器】
    nodejs:fs文件系统模块
    03 【资源处理】
    AcrelEMS-BP生物制药工厂能效管理系统
    [Leetcode]用队列实现栈
    TSINGSEE青犀基于AI视频识别技术的平安校园安防视频监控方案
  • 原文地址:https://blog.csdn.net/a232884c/article/details/126136390