经常要用到java来产生随机字符串,感觉org.apache.commons.lang.RandomStringUtils还是不够用,比如不能按照指定格式来产生随机串,因此自己写了一个RandomString类,放在这里记录一下。源文件在用法简介的后面。
一、用法简介:
//产生4位的,由数字和大写字母组成的随机字符串;
String s1=RandomString.get(4);
//产生5位的,由数字组成的随机字符串;
String s2=RandomString.getNum(5);
//产生6位的,由十六进制字符组成的随机字符串;
String s3=RandomString.getHex(6);
//产生如下格式的随机字符串“{E3-K91Z}”,“?”表示一个占位符;
String s4=RandomString.get("{??-????}");
//产生如下格式的随机字符串“Are you W5B?”,可以自己指定临时占位符,这里用“#”;
String s5=RandomString.get("Are you ###?",'#');
二、源文件:
public class RandomString {
private static final String src_number = "0123456789";
private static final String src_lower = "abcdefghijklmnopqrstuvwxyz";
private static final String src_upper = src_lower.toUpperCase();
private static final String src_hex_lower = "0123456789abcdef";
private static final String src_hex_upper = src_hex_lower.toUpperCase();
private static final String esc_char = "?";
public static String get(int size) {
StringBuffer r = new StringBuffer(size);
String src = src_number + src_upper;
for (int i = 0; i < size; i++) {
r.append(getRandomChar(src));
}
return r.toString();
}
public static String get(String format) {
StringBuffer r = new StringBuffer(format.length());
String src = src_number + src_upper;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(esc_char)) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
public static String get(String format, char esc) {
StringBuffer r = new StringBuffer(format.length());
String src = src_number + src_upper;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(String.valueOf(esc))) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
public static String getNum(int size) {
StringBuffer r = new StringBuffer(size);
String src = src_number;
for (int i = 0; i < size; i++) {
r.append(getRandomChar(src));
}
return r.toString();
}
public static String getNum(String format) {
StringBuffer r = new StringBuffer(format.length());
String src = src_number;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(esc_char)) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
//Java中文网:http://www.javaweb.cc
public static String getNum(String format, char esc) {
StringBuffer r = new StringBuffer(format.length());
String src = src_number;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(String.valueOf(esc))) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
public static String getHex(int size) {
StringBuffer r = new StringBuffer(size);
String src = src_hex_upper;
for (int i = 0; i < size; i++) {
r.append(getRandomChar(src));
}
return r.toString();
}
public static String getHex(String format) {
StringBuffer r = new StringBuffer(format.length());
String src = src_hex_upper;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(esc_char)) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
public static String getHex(String format, char esc) {
StringBuffer r = new StringBuffer(format.length());
String src = src_hex_upper;
for (int i = 0; i < format.length(); i++) {
String curr = String.valueOf(format.charAt(i));
if (curr.equalsIgnoreCase(String.valueOf(esc))) {
r.append(getRandomChar(src));
} else {
r.append(curr);
}
}
return r.toString();
}
//Java教程:http://www.javaweb.cc
private static final String getRandomChar(String src) {
if (null==src||"".equals(src)) {
return "";
}
return String.valueOf((src.charAt((int) (Math.random() * src.length()))));
}
}
|
|