package com.ycl.common.util;
|
|
import java.util.Random;
|
|
public class RandomStringUtil {
|
public static final String NUMBERS = "0123456789";
|
public static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
|
public static final String CAPITAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
public static final String CAPITAL_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
public static final String CAPITAL_NUM_LOWER = "0123456789abcdefghijklmnopqrstuvwxyz";
|
public static final String ALL = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
/**
|
* 生成指定长度的随机字符串
|
*
|
* @param length
|
* @return
|
*/
|
public static String getRandomString(int length, String base) { // length表示生成字符串的长度
|
int baseLength = base.length();
|
Random random = new Random();
|
StringBuffer sb = new StringBuffer("");
|
for (int i = 0; i < length; i++) {
|
int number = random.nextInt(baseLength);
|
sb.append(base.charAt(number));
|
}
|
return sb.toString();
|
}
|
|
public static void main(String[] args) {
|
System.out.println(getRandomString(6, RandomStringUtil.NUMBERS));
|
}
|
}
|