Java生成随机字符串

/***
 * 随机生成字符串
 * @length 字符长度
 * @return String
 * */
public static String getRandomString(int length){
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for(int i = 0 ; i < length; ++i){
        int number = random.nextInt(62);
        sb.append(str.charAt(number));
    }
    return sb.toString();
}
 
//调用代码
public static void main(String[] args) {
    String code = getRandomString(6);
    System.out.println(code);
}
欢迎转载,原文地址:http://www.lrfun.com/html/technology/java/2017/0921/123.html

上一篇:Java数字金额转为大写金额(正则表达式)
下一篇:Java运用时间戳对比两个时间的大小