【Java】生成随机字符串
package com.runsky.utils;
import java.util.Random;
public class GetRandom {
private static final String[] GENERATE_SOURCE = new String[]{"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"};
public static String generateByRandom(final int length) {
StringBuilder randomSb = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equals(charOrNum)) {
// 判断字母大小写
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
randomSb = randomSb.append((char) (choice + random.nextInt(26)));
} else {
randomSb = randomSb.append(random.nextInt(10));
}
}
return randomSb.toString();
}
public static void main(String[] args) {
String s = generateByRandom(12);
System.out.println(s);
}
}
作者:小魁jking
出处:https://www.cnblogs.com/wangjinkui/
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
出处:https://www.cnblogs.com/wangjinkui/
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任