生成指定长度的随机字符串

一、该方法用来生成指定长度的随机字符串(JAVA)

点击查看代码
/**
 * @Classname RandomStringGeneratorUtil
 * @Description 获取一定长度的随机字符串
 * @Date 2022/3/3 10:28
 * @Created by 小郭
 */
public class RandomStringGeneratorUtil {

    /**
     * 获取一定长度的随机字符串
     * @param length
     * @return
     */
    public static String getRandomStringByLength(int length){
        String base="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <length ; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String a = getRandomStringByLength(32);
        System.out.println("a = " + a);
    }
}
posted @ 2022-03-26 10:22  青喺半掩眉砂  阅读(232)  评论(0编辑  收藏  举报