生成字符串

生成字符串

import java.util.Random;

/**
 * 生成字符串
 */
public class RandomUtil {

    /**
     * 生成 数字和大小写字母的字符串
     *
     * @param length
     * @return
     */
    public String getCharAndNum(int length) {
         
        if (length <= 0){
            return "";
        }
        return this.charAndNum(length);
    }



    private String charAndNum(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            //random.nextInt(2) 0 或1
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) { //字符串
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; //取得大写字母还是小写字母
//                val += (char) (choice + random.nextInt(26));
                sb.append((char) (choice + random.nextInt(26)));

            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                sb.append(random.nextInt(10));
            }
        }
        return sb.toString();
    }

    /**
     * 获得大写字母的字符串
     *
     * @param length 需要的长度 当length 小于等于0时 返回 空字符""
     * @return
     */
    public String getUpperCaseChar(int length) {
        if (length <= 0){
            return "";
        }
        return this.upperCaseChar(length);
    }

    /**
     * 生成大写字母的字符串
     *
     * @param length 需要的字符长度
     * @return
     */
    private String upperCaseChar(int length) {
        StringBuffer result = new StringBuffer("");
        Random random = new Random();
        for (int i = 0; i < length; i++) {
//        65-90 代表 A-Z 大写字母char符号值
            int ran = random.nextInt(26) + 65;
            result.append((char) ran);
        }
        return result.toString();
    }


    /**
     * 获得小写字母的字符串
     *
     * @param length 需要的字符长度
     * @return
     */
    public String getLowerCaseChar(int length) {
        if (length <= 0){
            return "";
        }
        return this.lowerCaseChar(length);
    }

    /**
     * 生成小写字母的字符串
     *
     * @param length 需要的字符长度
     * @return
     */
    private String lowerCaseChar(int length) {
        StringBuffer result = new StringBuffer("");
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            //97-122 代表 A-Z 小写字母char符号值
            int ran = random.nextInt(26) + 97;
            result.append((char) ran);
        }
        return result.toString();
    }

    /**
     * 获取纯数字字符串
     * 返回的字符串可能会以 0开头
     *
     * @param length 长度
     */
    public String getNum(int length) {
        if (length <= 0){
            return "";
        }
        return this.num(length, false);
    }

    /**
     * 获取纯数字字符串
     *
     * @param length 长度
     * @param bl     返回的字符串是否可以以0开头 false 不能
     */
    public String getNum(int length, boolean bl) {
        if (length <= 0){
            return "";
        }
        return this.num(length, bl);
    }

    private String num(int length, boolean bl) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int r = random.nextInt(10);
            while (i == 0 && bl && r == 0) {
                System.out.println("r22 =" + r);
                r = random.nextInt(10);
            }
            System.out.println("r =" + r);
            sb.append(r);
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        RandomUtil t = new RandomUtil();
        Random random = new Random();
        System.out.println(t.getNum(15));

    }
}

你可能感兴趣的:(java工具类)