当前位置:网站首页>Random string tool class randomstringutils detailed explanation
Random string tool class randomstringutils detailed explanation
2022-04-22 05:40:00 【Siege lion with dream】
Preface
There are many scenarios in project development that require us to generate some non repetitive strings , Use UUID It's a situation we often use , however UUID The length is longer , And the length can't be customized , In the actual use process, there may be some inconvenient places , Today we are going to introduce this tool class , You can freely configure the length of the generated string 、 Composition of strings 、 Generation form, etc , This tool is Apache A very practical class in the public package :RandomStringUtils, Next, let's introduce each method of this tool class in detail , So that everyone can use it more conveniently in daily development .
Depend on the configuration
Here we go through maven To introduce the corresponding Jar package , Corresponding pom The documents are as follows
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
The source code parsing
random Method ( The random content contains all the codes )
In a tool class ,random There are seven overloaded methods , They are as follows :
# The method has only one parameter , Randomly generate a specified number of characters as the return result , Character format randomly choose from all character formats , Chinese character format will generate a lot of garbled code next time , Use with caution
public static String random(final int count) {
return random(count, false, false);
}
# Two parameters of the method , The first parameter represents the number of characters of the generated string , The second parameter represents the character range contained in the generated string , It can be for NULL, But it can't be empty , If the second parameter is NULL Words , From all character formats , Use with caution
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());
}
# The method has three parameters , The first parameter represents the number of characters of the produced string ; The second parameter indicates whether the generated string contains English letters , If true It means to include ; The third parameter indicates whether the generated string contains numbers , If true It means to include
public static String random(final int count, final boolean letters, final boolean numbers) {
return random(count, 0, 0, letters, numbers);
}
# The method contains two parameters , The first parameter represents the number of characters of the generated string , The second parameter is an array of characters , The generated string is randomly selected from the characters of the array , The second parameter can be NULL, If NULL, Select... Randomly from all characters , Use with caution
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);
}
# The method contains five parameters , The first parameter represents the number of characters of the generated string , The second parameter indicates the starting position of the generated character in the whole character set , The third parameter represents the end position of the generated character in the whole character set , The fourth parameter indicates whether the generated string contains English letters , If true It means to include ; The fifth parameter indicates whether the generated string contains numbers , If true It means to include
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);
}
# The method contains six parameters , The first parameter represents the number of characters of the generated string , The second parameter represents the position of the generated character at the beginning of the subscript of the sixth parameter string array , The third parameter represents the position of the generated character at the end of the subscript of the sixth parameter string array , The fourth parameter indicates whether the generated string contains English letters , If true It means to include ; The fifth parameter indicates whether the generated string contains numbers , If true It means to include ; The sixth parameter is a character array , The generated string is randomly obtained from the specified start and end positions of this character array , It can be for NULL, If NULL Get... From all characters
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);
}
# The method contains seven parameters , All of the above random The final realization of distribution , This method allows the use of user-defined Random Method , Use a singleton Random This method can prevent the same Random The sequence randomly repeats the string
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 Method ( The random content contains the encoding of upper and lower case letters )
# This method randomly generates a string containing upper and lower case letters , A parameter indicates the number of letters contained in the string
public static String randomAlphabetic(final int count) {
return random(count, true, false);
}
# This method randomly generates a string containing upper and lower case letters , But the length of the string is randomly generated , The random range is the two parameters of the method , The first represents a random minimum , The second represents a random maximum
public static String randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAlphabetic(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomAlphanumeric Method ( Random content includes upper and lower case letters and numbers [0~9] The coding )
## This method randomly generates a string containing upper and lower case letters and numbers , A parameter indicates the number of letters and numbers contained in the string
public static String randomAlphanumeric(final int count) {
return random(count, true, true);
}
# This method randomly generates a string containing upper and lower case letters and numbers , But the length of the string is randomly generated , The random range is the two parameters of the method , The first represents a random minimum , The second represents a random maximum
public static String randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomAscii Method ( The random content contains only Ascii code )
# Randomly produce one that contains only Ascii Encoded string , Because the coding range is ASCII, So their character range is 32-127 Between , A parameter limits the number of characters contained in the string
public static String randomAscii(final int count) {
return random(count, 32, 127, false, false);
}
# Randomly produce one that contains only Ascii Encoded string , Because the coding range is ASCII, So their character range is 32-127 Between , The length of this string is random , The random range is between the first and second parameters of the method
public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive) {
return randomAscii(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomGraph Method (ASCII in 33-126 The characters of , Contains punctuation marks 、 Case letters 、 Numbers )
# The output contains punctuation marks randomly 、 Case letters 、 A string of numbers , The length of the string is limited by the first parameter of the method
public static String randomGraph(final int count) {
return random(count, 33, 126, false, false);
}
# The output contains punctuation marks randomly 、 Case letters 、 A string of numbers , The length of the string is randomly generated between the two parameters of the method
public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) {
return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomNumeric Method ( A string containing only numbers )
# Randomly generate a string containing only numbers , The first parameter represents the length of the string
public static String randomNumeric(final int count) {
return random(count, false, true);
}
# Randomly generate a string containing only numbers , The length of the string is randomly generated between the first and second parameters of the method
public static String randomNumeric(final int minLengthInclusive, final int maxLengthExclusive) {
return randomNumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
randomPrint Method (ASCII in 32-126 The characters of , Contains punctuation marks 、 Case letters 、 Numbers and spaces )
# The output contains punctuation marks randomly 、 Case letters 、 Strings of numbers and spaces , The length of the string is limited by the first parameter of the method
public static String randomPrint(final int count) {
return random(count, 32, 126, false, false);
}
# Generate random, including punctuation 、 Case letters 、 Strings of numbers and spaces , The length of the string is randomly generated between the two parameters of the method
public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) {
return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive));
}
summary
Through the above combing , We will find that , This tool class provides great convenience for us to generate random strings in our daily development , I hope the above explanation , Can give you some help in daily development
版权声明
本文为[Siege lion with dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220532240509.html
边栏推荐
- C language version: binary tree leaf node and non leaf node solution
- Application of C language stack: binary conversion
- CopyOnWriteArrayList的使用场景
- JVM探究
- MySQL存储时间的最佳实践
- Do you know the implementation of strcpy?
- TensorFlow
- Force buckle - 300 Longest increasing subsequence
- List中set方法和add方法的区别
- Redis缓存负载均衡使用的一致性哈希算法
猜你喜欢

雷达设备(贪心)

After unity is connected to the ilruntime, the package method under packages is applied to the hot engineering application, such as unity RenderPipelines. Core. Runtime package

mysql中on duplicate key update 使用详解

Force buckle - 354 Russian Doll envelope problem

JVM探究

C language version: the pre order, middle order and post order non recursive traversal of binary tree

Pratique du langage C (2) - - mise en oeuvre de l'addition polynomiale par liste liée

Unreal engine sequence effect and audio binding trigger

Domain based approach - score prediction

C language version: binary tree leaf node and non leaf node solution
随机推荐
【转】MySQL:InnoDB一棵B+树可以存放多少行数据?
POI 和 EasyExcel练习
Method for coexistence of Keil-C51 and keil arm
可重入锁卖票示例
Force buckle - 354 Russian Doll envelope problem
4.打印表格
程序的编译(预处理操作)+ 链接
Hloj 1936 covered with squares
MySQL数据库基础
MySQL存储时间的最佳实践
LockSupport.park和unpark,wait和notify
Five important properties of binary tree
11.a==b?
TensorFlow
MySQL表的增删改查
C language practice (2) -- polynomial addition with linked list
C language version: the pre order, middle order and post order non recursive traversal of binary tree
MySQL函数及练习题(二)
IDEA debug高级操作
2022.4.21-----leetcode. eight hundred and twenty-four