Friday, September 2, 2011

Java Using Apache Jar : Random String from specific characters


Apache came with implementation in creating a random value using the RandomStringUtils class.

 RandomStringUtils : Operations for random Strings.

Creates a random string whose length is the number of characters specified.


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.
Parameters:
count - the length of random string to create
chars - the String containing the set of characters to use, may be null
Returns:
the random string
Throws:
IllegalArgumentException - if count < 0.

Example: RandomStringUtils.random(6, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-*/!@#$%^&*()");

The above example gives the random string whose length is 6.

Refer the URL for more details http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/RandomStringUtils.html

No comments :

Post a Comment