public class RandomStringUtils extends Object
Operations for random String
s.
Currently private high surrogate characters are ignored. These are Unicode characters that fall between the values 56192 (db80) and 56319 (dbff) as we don't know how to handle them. High and low surrogates are correctly dealt with - that is if a high surrogate is randomly chosen, 55296 (d800) to 56191 (db7f) then it is followed by a low surrogate. If a low surrogate is chosen, 56320 (dc00) to 57343 (dfff) then it is placed after a randomly chosen high surrogate.
#ThreadSafe#
Constructor and Description |
---|
RandomStringUtils()
RandomStringUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static String |
random(int count)
Creates a random string whose length is the number of characters
specified.
|
static String |
random(int count,
boolean letters,
boolean numbers)
Creates a random string whose length is the number of characters
specified.
|
static String |
random(int count,
char... chars)
Creates a random string whose length is the number of characters
specified.
|
static String |
random(int count,
int start,
int end,
boolean letters,
boolean numbers)
Creates a random string whose length is the number of characters
specified.
|
static String |
random(int count,
int start,
int end,
boolean letters,
boolean numbers,
char... chars)
Creates a random string based on a variety of options, using
default source of randomness.
|
static String |
random(int count,
int start,
int end,
boolean letters,
boolean numbers,
char[] chars,
Random random)
Creates a random string based on a variety of options, using
supplied source of randomness.
|
static String |
random(int count,
String chars)
Creates a random string whose length is the number of characters
specified.
|
static String |
randomAlphabetic(int count)
Creates a random string whose length is the number of characters
specified.
|
static String |
randomAlphanumeric(int count)
Creates a random string whose length is the number of characters
specified.
|
static String |
randomAscii(int count)
Creates a random string whose length is the number of characters
specified.
|
static String |
randomNumeric(int count)
Creates a random string whose length is the number of characters
specified.
|
public RandomStringUtils()
RandomStringUtils
instances should NOT be constructed in
standard programming. Instead, the class should be used as
RandomStringUtils.random(5);
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static String random(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of all characters.
count
- the length of random string to createpublic static String randomAscii(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of characters whose
ASCII value is between 32
and 126
(inclusive).
count
- the length of random string to createpublic static String randomAlphabetic(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of alphabetic characters.
count
- the length of random string to createpublic static String randomAlphanumeric(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of alpha-numeric characters.
count
- the length of random string to createpublic static String randomNumeric(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of numeric characters.
count
- the length of random string to createpublic static String random(int count, boolean letters, boolean numbers)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
count
- the length of random string to createletters
- if true
, generated string may include
alphabetic charactersnumbers
- if true
, generated string may include
numeric characterspublic static String random(int count, int start, int end, boolean letters, boolean numbers)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
count
- the length of random string to createstart
- the position in set of chars to start atend
- the position in set of chars to end beforeletters
- if true
, generated string may include
alphabetic charactersnumbers
- if true
, generated string may include
numeric characterspublic static String random(int count, int start, int end, boolean letters, boolean numbers, char... chars)
Creates a random string based on a variety of options, using default source of randomness.
This method has exactly the same semantics as
random(int,int,int,boolean,boolean,char[],Random)
, but
instead of using an externally supplied source of randomness, it uses
the internal static Random
instance.
count
- the length of random string to createstart
- the position in set of chars to start atend
- the position in set of chars to end beforeletters
- only allow letters?numbers
- only allow numbers?chars
- the set of chars to choose randoms from.
If null
, then it will use the set of all chars.ArrayIndexOutOfBoundsException
- if there are not
(end - start) + 1
characters in the set array.public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random random)
Creates a random string based on a variety of options, using supplied source of randomness.
If start and end are both 0
, start and end are set
to ' '
and 'z'
, the ASCII printable
characters, will be used, unless letters and numbers are both
false
, in which case, start and end are set to
0
and Integer.MAX_VALUE
.
If set is not null
, characters between start and
end are chosen.
This method accepts a user-supplied Random
instance to use as a source of randomness. By seeding a single
Random
instance with a fixed seed and using it for each call,
the same random sequence of strings can be generated repeatedly
and predictably.
count
- the length of random string to createstart
- the position in set of chars to start atend
- the position in set of chars to end beforeletters
- only allow letters?numbers
- only allow numbers?chars
- the set of chars to choose randoms from, must not be empty.
If null
, then it will use the set of all chars.random
- a source of randomness.ArrayIndexOutOfBoundsException
- if there are not
(end - start) + 1
characters in the set array.IllegalArgumentException
- if count
< 0 or the provided chars array is empty.public static String random(int count, String chars)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of characters specified by the string, must not be empty. If null, the set of all characters is used.
count
- the length of random string to createchars
- the String containing the set of characters to use,
may be null, but must not be emptyIllegalArgumentException
- if count
< 0 or the string is empty.public static String random(int count, char... chars)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of characters specified.
count
- the length of random string to createchars
- the character array containing the set of characters to use,
may be nullIllegalArgumentException
- if count
< 0.Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.