java中判断字符串是否为数字(正整数)

标题:java中判断字符串是否为数字(正整数)

  1. 遍历s的每一个字符
   private boolean isNumeric(String s) {
    
    
        for (int i = 0; i < s.length(); i++) {
    
    
            char ch = s.charAt(i);
            if (!(ch >= '0' && ch <= '9')) {
    
    
                return false;
            }
        }

        return true;
    }

2.自己编写函数,使用正则表达式,

    private boolean isNumeric(String s) {
    
    
        if (s != null && !"".equals(s.trim())){
    
    
            return s.matches("^[0-9]*$");
        } else {
    
    
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45986454/article/details/127239546
今日推荐