工具类:随机字符串

开始

package com.xjxcc.util;

import java.util.Random;
 
/**
 * 获取随机数字、小写字母、大写字母、特殊字符、汉字字符
 * @author liguoqing
 *
 */
public class RandomStringUtils{   
    private final static String splitStr = " "; // 分割符   
     
    /**
     * 类型:数字
     */
    public final static int TYPE_NUMBER = 1 ;
    /**
     * 类型:小写字母
     */
    public final static int TYPE_LOWER_LETTER = 2 ;
    /**
     * 类型:大写字母
     */
    public final static int TYPE_UPPER_LETTER = 3 ;
    /**
     * 类型:特殊字符
     */
    public final static int TYPE_SPECIAL_CHARACTER = 4 ;
    /**
     * 类型:汉字字符
     */
    public final static int TYPE_CHINESE_CHARACTER = 5 ;
     
   
    /**
     * 取数字字符串 用 splitStr 分割   
     */
    private static String getNumberString() {   
        StringBuffer buf = new StringBuffer();   
        for (int i = 0; i < 10; i++)   
        {   
            buf.append(String.valueOf(i));   
            buf.append(splitStr);   
        }   
        return buf.toString();   
    }   
   
    /**
     * 取大写字母字符串 用 splitStr 分割    
     */
    private static String getUppercase() {   
        StringBuffer buf = new StringBuffer();   
        for (int i = 0; i < 26; i++)   
        {   
            buf.append(String.valueOf((char) ('A' + i)));   
            buf.append(splitStr);   
        }   
        return buf.toString();   
    }   
   
    /**
     * 取小写字母字符串 用 splitStr 分割   
     */
    private static String getLowercase() {   
        StringBuffer buf = new StringBuffer();   
        for (int i = 0; i < 26; i++)   
        {   
            buf.append(String.valueOf((char) ('a' + i)));   
            buf.append(splitStr);   
        }   
        return buf.toString();   
    }   
   
    /**
     * 取特殊字符串 用 splitStr 分割   
     */
    private static String getSpecialString() {   
        String str = "~@#$%^&*()_+|\\=-`";   
        StringBuffer buf = new StringBuffer();   
        for (int i = 0; i < str.length(); i++)   
        {   
            buf.append(str.substring(i, i + 1));   
            buf.append(splitStr);   
        }   
        return buf.toString();   
    }  
     
    /**
     * 取汉字字符串 用 splitStr 分割
     */
    private static String getChineseString(){
        StringBuffer buf = new StringBuffer();   
        int min = (int)(4*Math.pow(16, 3)+14*Math.pow(16, 2)); // 汉字ASCII码最小值
        int max = (int)(9*Math.pow(16, 3)+15*Math.pow(16, 2)+10*Math.pow(16, 1))+5;  // 汉字ASCII码最大值
         
        for (int i = 0; i < 100; i++) {
            int randomNum = (int)(min + Math.random() * (max - min + 1));
            buf.append((char)randomNum);//ASCII码转换为字符(汉字)
            buf.append(splitStr);
        }
         
        return buf.toString();
    }
   
    /**
     * 根据所取的字符串类型连接相应的字符串并返回    
     */
    private static String getString(int type) {   
        StringBuffer pstr = new StringBuffer();   
        if (type == RandomStringUtils.TYPE_NUMBER)   
            pstr.append(getNumberString());   
        if (type == RandomStringUtils.TYPE_LOWER_LETTER)   
            pstr.append(getLowercase());   
        if (type == RandomStringUtils.TYPE_UPPER_LETTER)   
            pstr.append(getUppercase());   
        if (type == RandomStringUtils.TYPE_SPECIAL_CHARACTER)   
            pstr.append(getSpecialString());   
        if (type == RandomStringUtils.TYPE_CHINESE_CHARACTER)   
            pstr.append(getChineseString());   
        return pstr.toString();   
    }   
   
    /**  
     * 取随机字符串  
     *   
     * @param length   返回随机字符串的长度  
     * @param type   要取的字符串类型:   1、取数字   2、取小写字母   3、取大写字母  4、取特殊字符   5、取汉字字符
     * @return String 随机字符串  
     */  
    public static String getRandomString(int length, int type) {
        String allStr = getString(type);   
        String[] arrStr = allStr.split(splitStr);   
        StringBuffer pstr = new StringBuffer();   
        if (length > 0) {   
            for (int i = 0; i < length; i++)   
            {   
                pstr.append(arrStr[new Random().nextInt(arrStr.length)]);   
            }   
        }   
        return pstr.toString();   
    }   
   
     
    public static void main(String[] args) {
        System.out.println(RandomStringUtils.getRandomString(10, RandomStringUtils.TYPE_NUMBER));//10位数字
        System.out.println(RandomStringUtils.getRandomString(9, RandomStringUtils.TYPE_LOWER_LETTER));//9位小写字母
        System.out.println(RandomStringUtils.getRandomString(8, RandomStringUtils.TYPE_UPPER_LETTER));//8位大写字母
        System.out.println(RandomStringUtils.getRandomString(7, RandomStringUtils.TYPE_SPECIAL_CHARACTER));//7位特殊字符
        System.out.println(RandomStringUtils.getRandomString(6, RandomStringUtils.TYPE_CHINESE_CHARACTER));//6位汉字字符
    }
     
}

结束

猜你喜欢

转载自blog.csdn.net/liguoqingxjxcc/article/details/81666589