public class RandomStringGenerator extends Object
This class doesn't generate secure strings. So please use SecureRandom class if you want to use with such a purpose.
| 构造器和说明 |
|---|
RandomStringGenerator()
Instantiate generator with a default number of upper limit for regex quantifiers (for example
*, + and etc; default value is 10) and a default instance of Random. |
RandomStringGenerator(int numOfUpperLimit)
Instantiate generator with a number of upper limit for regex quantifiers (for example
*
, + and etc) and a default instance of Random. |
RandomStringGenerator(Random random)
Instantiate generator with a default number of upper limit for regex quantifiers (for example
*, + and etc; default value is 10) and an instance of Random. |
RandomStringGenerator(Random random,
int numOfUpperLimit)
Instantiate generator with a number of upper limit for regex quantifiers (for example
*
, + and etc) and an instance of Random. |
| 限定符和类型 | 方法和说明 |
|---|---|
String |
generateByRegex(String regex)
Generate random string from regular expression.
|
String |
generateFromPattern(String pattern)
Generate random string from pattern.
|
int |
getNumOfUpperLimit()
Get number of upper limit for regex quantifiers, for example
*, + and etc. |
void |
setNumOfUpperLimit(int numOfUpperLimit)
Set number of upper limit for regex quantifiers, for example
*, + and etc. |
public RandomStringGenerator()
*, + and etc; default value is 10) and a default instance of Random.public RandomStringGenerator(int numOfUpperLimit)
*
, + and etc) and a default instance of Random.numOfUpperLimit - Number of upper limit for quantifierspublic RandomStringGenerator(Random random)
*, + and etc; default value is 10) and an instance of Random.random - Instance of Randompublic RandomStringGenerator(Random random, int numOfUpperLimit)
*
, + and etc) and an instance of Random.random - Instance of RandomnumOfUpperLimit - Number of upper limit for quantifierspublic String generateFromPattern(String pattern)
You can use following characters as pattern.
c : Any Latin lower-case characterC : Any Latin upper-case charactern : Any digit [0-9]! : A symbol character [~`!@$%^&*()-_+= []|\:;"'.<>?/#,]. : Any of the aboves : A "salt" character [A-Za-z0-9./]b : An ASCIII character which has code from 0 to 255e.g.
RandomStringGenerator generator = new RandomStringGenerator();
// generates random string (e.g. "aB4@X.Ç")
String randomString = generator.generateFromPattern("cCn!.sb");
pattern - Pattern stringpublic String generateByRegex(String regex)
You can use following meta characters as regex.
\w : Alphanumeric + "_" [A-Za-z0-9_]\d : Digits [0-9]\W : Printable characters other than those in \w\D : Printable characters other than those in \d\s : Whitespace characters [ \t]\S : Printable characters. : Printable characters[] : Character classes (Example of usage [a-zA-Z]){}: Repetition* : Same as {0,}? : Same as {0,1}+ : Same as {1,}e.g.
RandomStringGenerator generator = new RandomStringGenerator();
// generates random string (e.g. "a5B123 18X")
String randomString = generator.generateByRegex("\\w+\\d\*\s[0-9]{0,3}X");
regex - Pattern based on regular expressionpublic int getNumOfUpperLimit()
*, + and etc.public void setNumOfUpperLimit(int numOfUpperLimit)
*, + and etc.numOfUpperLimit - Number of upper limit for quantifiersCopyright © 2017–2019. All rights reserved.