zoukankan      html  css  js  c++  java
  • 产生验证码时随机获取数字与字母的序列

    /** 
     * 获取随机字母数字组合 
     *  
     * @param length 
     *            字符串长度 
     * @return 
     */  
    public static String getRandomCharAndNumr(Integer length) {  
        String str = "";  
        Random random = new Random();  
        for (int i = 0; i < length; i++) {  
            boolean b = random.nextBoolean();  
            if (b) { // 字符串  
                // int choice = random.nextBoolean() ? 65 : 97; 取得65大写字母还是97小写字母  
                str += (char) (65 + random.nextInt(26));// 取得大写字母  
            } else { // 数字  
                str += String.valueOf(random.nextInt(10));  
            }  
        }  
        return str;  
    }  
      
    /** 
     * 验证随机字母数字组合是否纯数字与纯字母 
     *  
     * @param str 
     * @return true 是 , false 否 
     */  
    public static boolean isRandomUsable(String str) {  
        // String regExp =  
        // "^[A-Za-z]+(([0-9]+[A-Za-z0-9]+)|([A-Za-z0-9]+[0-9]+))|[0-9]+(([A-Za-z]+[A-Za-z0-9]+)|([A-Za-z0-9]+[A-Za-z]+))$";  
        String regExp = "^([0-9]+)|([A-Za-z]+)$";  
        Pattern pat = Pattern.compile(regExp);  
        Matcher mat = pat.matcher(str);  
        return mat.matches();  
    }  
    
  • 相关阅读:
    python3 sorted()函数解析
    MySql 关键字
    python的 a,b=b,a+b 和 a=b b=a+b 的区别
    python3 all() 函数用于检查实参
    Python3 urllib模块
    Python3 shutil模块
    Python3 sys模块
    Python 异常处理和断言
    Python3 os模块
    Pytho3 file open方法
  • 原文地址:https://www.cnblogs.com/jxgapyw/p/5045308.html
Copyright © 2011-2022 走看看