zoukankan      html  css  js  c++  java
  • java 判断是否是数字

    View Code
    import java.util.regex.Pattern;
    
    public class NumberTest {
       /**
        * 正则表达式
        * @param str
        * @return
        */
        public static boolean isNumberic(String str){
            Pattern pattern = Pattern.compile("[0-9]*");
            return pattern.matcher(str).matches();
        }
        /**
         * java自带函数
         * @param str
         * @return
         */
        public static boolean isNumberic2(String str){
            for(int i = str.length();--i>=0;){
                if(!Character.isDigit(str.charAt(i))){
                    return false;
                }
            }
            return true;
        }
        /**
         * 正则表达式
         * @param str
         * @return
         */
       public static boolean isNumberic3(String str){
           if(str.matches("\\d*")){
               return true;
           }
           return false;
       }
       /**
        * 判断ASCII
        * @param str
        * @return
        */
       public static boolean isNumberic4(String str){
           for(int i = str.length();--i>=0;){
               int chr = str.charAt(i);
               if(chr < 48 || chr > 57){
                   return false;
               }
           }
           return true;
       }
       /**
        * 逐个判断str中的字符是否是0-9
        * @param str
        * @return
        */
       public static boolean isNumberic5(String str){
           final String number = "0123456789";
           for(int i = 0;i<str.length();i++){
               if(number.indexOf(str.charAt(i)) == -1){
                   return false;
               }
           }
           return true;
       }
       
       public static void main(String[] args) {
        String str = "123";
         boolean result1 = isNumberic(str);
         boolean result2 = isNumberic2(str);
         boolean result3 = isNumberic3(str);
         boolean result4 = isNumberic4(str);
         boolean result5 = isNumberic5(str);
         System.err.println(result1);
         System.err.println(result2);
         System.err.println(result3);
         System.err.println(result4);
         System.err.println(result5);
    }
    }
  • 相关阅读:
    Javascript事件处理程序的3种方式
    JS原生AJAX
    所谓的渐进增强,优雅降级?
    1059 老师的苦恼
    HTML5 参数传递
    HDU 5289 Assignment(二分+RMQ-ST)
    HDU 3333 Turing Tree(离线树状数组)
    mac下 mysql 插入中文乱码解决
    校园商铺-7商品类别模块-5商品类别删除后端开发
    校园商铺-7商品类别模块-3商品类别添加后端开发
  • 原文地址:https://www.cnblogs.com/hujia/p/2751519.html
Copyright © 2011-2022 走看看