您的位置:首页 > 其它

随机生成字符串

2016-10-22 09:28 351 查看
随机生成字符串需要导入包import java.util.Random;

以下是两种随机生成字符串的方法:

<span style="font-size:18px;">import java.util.Random;
import java.util.UUID;

/**
* 生产指定长度随机字符串a-z,A-Z,0-9
* @author happyqing
* @since 2016.5.30
*/
public class RandomString {

/**
* 获取随机字符串 a-z,A-Z,0-9
*
* @param length
*            长度
* @return
*/
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);// [0,62)
sb.append(str.charAt(number));
}
return sb.toString();
}

/**
* JAVA获得0-9,a-z,A-Z范围的随机数
*
* @param length
*            随机数长度
* @return String
*/
public static String getRandomChar(int length) {
char[] chr = { '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',
'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' };
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(chr[random.nextInt(62)]);
}
return buffer.toString();
}

public static void main(String[] args) {
System.out.println(getRandomString(32));
System.out.println(getRandomChar(32));
//UUID
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
System.out.println(uuid);
}
}  </span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: