生成指定长度的随机字符串
一、该方法用来生成指定长度的随机字符串(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);
}
}
本文来自博客园,作者:青喺半掩眉砂,转载请注明原文链接:https://www.cnblogs.com/xiaoguo-java/p/16057863.html