zoukankan      html  css  js  c++  java
  • java判断字符串中是否包含数字

    // 判断字符串包含数字 方法一
        public static boolean testIsNumMethodOne(String str) {
            boolean flag = false;
            String numStr = "0123456789";
            for (int i = 0; i < str.length(); i++) {
                String subStr = str.substring(i, i+1);
                if (numStr.contains(subStr)) {
                    flag = true;
                }
            }
            return flag;
        }
        
        // 判断字符串包含数字 方法二
        public static boolean testIsNumMethodTwo(String str) {
            boolean flag = false;
            Pattern pattern = Pattern.compile("[0-9]+");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                flag = true;
            }
            return flag;
        }
        
        // 判断字符串包含数字 方法三
        public static boolean testIsNumMethodThree(String str) {
            boolean flag = false;
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c > 48 && c < 57) {
                    flag = true;
                }
            }
            return flag;
        }
        
        // 判断字符串包含数字 方法四
        public static boolean testIsNumMethodFour(String str) {
            boolean flag = false;
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (Character.isDigit(c)) {
                    flag = true;
                }
            }
            return flag;
        }
  • 相关阅读:
    线性代数回顾+深化(未完成版)
    HIT OS2020 Spring Lab2
    选择
    工业互联网
    leetcode-200 岛屿数量
    记网易面试题<二>
    记网易面试题《一》
    leetecode-14-最长公共子串-简单
    leetcode-1012 至少有1位重复的数字
    协程
  • 原文地址:https://www.cnblogs.com/alphajuns/p/15730597.html
Copyright © 2011-2022 走看看