前言
项目开发中有很多场景需要我们生成一些不重复的字符串,使用UUID是我们经常使用的一种情况,但是UUID的长度比较长,而且长度是不可以自定义的,在实际的使用过程中可能会存在一些不方便的地方,今天我们要介绍的这个工具类,可以自由的配置生成的字符串的长度、字符串的组成、生成形式等,这个工具是Apache公共包里面的一个非常实用的一个类:RandomStringUtils,接下来我们详细的介绍一下这个工具类的每一个方法,让大家在日常开发中可以更加得心应手的使用。
介绍
RandomStringUtils是一个工具类,在Java中用于生成随机字符串。该类提供了一些方法来生成指定长度和字符集的随机字符串。
例如,RandomStringUtils.random(5)会生成一个长度为5的随机字符串,包含的字符可能是大小写字母和数字。RandomStringUtils.randomAlphanumeric(5)会生成一个长度为5的随机字母和数字的组合字符串。RandomStringUtils.randomAscii(4)会生成一个长度为4的随机ASCII字符串,包含从32到126的字符。
依赖配置
这里我们通过maven来引入这个工具类对应的Jar包,对应的pom文件如下
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
常用方法说明
- random(int length):生成一个指定长度的随机字符串,包含大小写字母和数字。
- randomAlphanumeric(int length):生成一个指定长度的随机字母和数字的组合字符串。
- randomAscii(int length):生成一个指定长度的随机ASCII字符串,包含从32到126的字符。
- random(int length, String chars):生成一个指定长度的随机字符串,包含指定的字符集中的字符。
注意
- 这些方法的作用是生成指定长度和字符集的随机字符串,方便在程序中进行测试、数据模拟等操作。
需要注意的是,RandomStringUtils使用的是伪随机数生成器,因此在多次生成随机字符串时,结果可能会重复。如果需要更高安全性的随机数生成,建议使用Java中的SecureRandom类。
源码解析
random方法 (随机内容包含全部的编码)
在工具类中,random方法共有七种重载实现,分别如下所示:
#该方法只有一个参数,随机生成指定个数的字符作为返回结果,字符格式从所有的字符格式中随机选择,中文字符格式下回生成很多乱码,慎用
public static String random(final int count) {
return random(count, false, false);
}
#该方法两个参数,第一个参数表示的是生成的字符串的字符个数,第二个参数表示的是生成的字符串中包含的字符范围,可以为 NULL, 但是不能为空,如果第二个参数为 NULL的话,则从所有的字符格式中随机,慎用
public static String random(final int count, final String chars) {
if (chars == null) {
return random(count, 0, 0, false, false, null, RANDOM);
}
return random(count, chars.toCharArray());
}
#该方法共有三个参数,第一个参数表示的是生产的字符串的字符个数;第二个参数表示生成的字符串中是否包含英文字母,如果true则表示包含;第三个参数表示生成的字符串中是否包含数字,如果true则表示包含
public static String random(final int count, final boolean letters, final boolean numbers) {
return random(count, 0, 0, letters, numbers);
}
#该方法中包含了两个参数,第一个参数表示的是生成的字符串的字符个数,第二个参数是一个字符数组,生成的字符串从该数组的字符中随机选择,第二个参数可以为NULL,如果为NULL,则从所有字符中随机选择,慎用
public static String random(final int count, final char... chars) {
if (chars == null) {
return random(count, 0, 0, false, false, null, RANDOM);
}
return random(count, 0, chars.length, false, false, chars, RANDOM);
}
#该方法包含了五个参数,第一个参数表示的是生成的字符串的字符个数,第二个参数表示了生成的字符在整个字符集中开始的位置,第三个参数表示了生成的字符在整个字符集中结束的位置,第四个参数表示生成的字符串中是否包含英文字母,如果true则表示包含;第五个参数表示生成的字符串中是否包含数字,如果true则表示包含
public static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers) {
return random(count, start, end, letters, numbers, null, RANDOM);
}
#该方法包含了六个参数,第一个参数表示的是生成的字符串的字符个数,第二个参数表示了生成的字符在第六个参数字符串数组的下标开始的位置,第三个参数表示了生成的字符在第六个参数字符串数组的下标结束的位置,第四个参数表示生成的字符串中是否包含英文字母,如果true则表示包含;第五个参数表示生成的字符串中是否包含数字,如果true则表示包含;第六个参数为一个字符数组,生成的字符串从这个字符数组的规定的起始位置和结束位置随机获取,可以为NULL,如果为NULL则从全部字符中获取
public static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers, final char... chars) {
return random(count, start, end, letters, numbers, chars, RANDOM);
}
#该方法包含了七个参数,是上述所有random发放的最终实现,该方法允许使用用户自定义的Random方法,使用一个单例的Random方法可以防止相同的Random序列随机出重复的字符串
public static String random(int count, int start, int end, final boolean letters, final boolean numbers,
final char[] chars, final Random random) {
if (count == 0) {
return StringUtils.EMPTY;
} else if (count < 0) {
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
}
if (chars != null && chars.length == 0) {
throw new IllegalArgumentException("The chars array must not be empty");
}
if (start == 0 && end == 0) {
if (chars != null) {
end = chars.length;
} else {
if (!letters && !numbers) {
end = Character.MAX_CODE_POINT;
} else {
end = 'z' + 1;
start = ' ';
}
}
} else {
if (end <= start) {
throw new IllegalArgumentException("Parameter end (" + end + ") must be greater than start (" + start + ")");
}
}
final int zero_digit_ascii = 48;
final int first_letter_ascii = 65;
if (chars == null && (numbers && end <= zero_digit_ascii
|| letters && end <= first_letter_ascii)) {
throw new IllegalArgumentException("Parameter end (" + end + ") must be greater then (" + zero_digit_ascii + ") for generating digits " +
"or greater then (" + first_letter_ascii + ") for generating letters.");
}
final StringBuilder builder = new StringBuilder(count);
final int gap = end - start;
while (count-- != 0) {
int codePoint;
if (chars == null) {
codePoint = random.nextInt(gap) + start;
switch (Character.getType(codePoint)) {
case Character.UNASSIGNED:
case Character.PRIVATE_USE:
case Character.SURROGATE:
count++;
continue;
}
} else {
codePoint = chars[random.nextInt(gap) + start];
}
final int numberOfChars = Character.charCount(codePoint);
if (count == 0 && numberOfChars > 1) {
count++;
continue;
}
if (letters && Character.isLetter(codePoint)
|| numbers && Character.isDigit(codePoint)
|| !letters && !numbers) {
builder.appendCodePoint(codePoint);
if (numberOfChars == 2) {
count--;
}
} else {
count++;
}
}
return builder.toString();
}
randomAlphabetic方法(随机内容包含大小写字母的编码)
#该方法随机生成一个包含大小写字母的字符串,一个参数表示该字符串包含的字母的个数
public static String randomAlphabetic(final int count) {
return random(count, true, false);
}
#该方法随机生成一个包含大小写字母的字符串,但是该字符串的长度是随机的生成的,随机的范围就是该方法的两个参数,第一个表示随机的最小值,第二个表示随机的最大值
public static String randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAlphabetic(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomAlphanumeric方法(随机内容包含大小写字母和数字[0~9]的编码)
##该方法随机生成一个包含大小写字母和数字的字符串,一个参数表示该字符串包含的字母和数字的个数
public static String randomAlphanumeric(final int count) {
return random(count, true, true);
}
#该方法随机生成一个包含大小写字母和数字的字符串,但是该字符串的长度是随机的生成的,随机的范围就是该方法的两个参数,第一个表示随机的最小值,第二个表示随机的最大值
public static String randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomAscii方法(随机内容仅包含Ascii编码)
#随机生产一个只包含Ascii编码的字符串,由于编码的范围是ASCII,所以他们字符范围在32-127之间,一个参数限定了这个字符串中包含的字符的个数
public static String randomAscii(final int count) {
return random(count, 32, 127, false, false);
}
#随机生产一个只包含Ascii编码的字符串,由于编码的范围是ASCII,所以他们字符范围在32-127之间,这个字符串的长度是随机的,随机的范围在该方法的第一个和第二个参数之间
public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAscii(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomGraph方法(ASCII中33-126的字符,包含了标点符号、大小写字母、数字)
# 输出随机的包含了标点符号、大小写字母、数字的字符串,字符串的长度由该方法的第一个参数来限定
public static String randomGraph(final int count) {
return random(count, 33, 126, false, false);
}
#输出随机的包含了标点符号、大小写字母、数字的字符串,字符串的长度随机产生与该方法的两个参数之间
public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) {
return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomNumeric方法(只包含数字的字符串)
#随机生成一个只包含数字的字符串,第一个参数表示了这个字符串的长度
public static String randomNumeric(final int count) {
return random(count, false, true);
}
#随机生成一个只包含数字的字符串,字符串的长度是由该方法的第一个和第二个参数之间随机生成的
public static String randomNumeric(final int minLengthInclusive, final int maxLengthExclusive) {
return randomNumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomPrint方法(ASCII中32-126的字符,包含了标点符号、大小写字母、数字和空格)
#输出随机的包含了标点符号、大小写字母、数字和空格的字符串,字符串的长度由该方法的第一个参数来限定
public static String randomPrint(final int count) {
return random(count, 32, 126, false, false);
}
#生成随机的包含了标点符号、大小写字母、数字和空格的字符串,字符串的长度随机产生与该方法的两个参数之间
public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) {
return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
总结
通过上述的梳理,我们会发现,这个工具类为我们在日常开发中生成随机字符串提供了非常大的便利,希望上面的讲解,可以给予您的日常开发一些帮助