How do I generate a random alpha-numeric string?
Category: commons.lang, viewed: 1382 time(s).
The code below show you how to use the commons-lang RandomStringUtils class to generate some random string data.
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsDemo {
public static void main(String[] args) {
//
// Creates a 64 chars length random string of number.
//
String result = RandomStringUtils.random(64, false, true);
System.out.println("random = " + result);
//
// Creates a 64 chars length of random alphabetic string.
//
result = RandomStringUtils.randomAlphabetic(64);
System.out.println("random = " + result);
//
// Creates a 32 chars length of random ascii string.
//
result = RandomStringUtils.randomAscii(32);
System.out.println("random = " + result);
//
// Creates a 32 chars length of string from the defined array of
// characters including numeric and alphabetic characters.
//
result = RandomStringUtils.random(32, 0, 20, true, true, "qw32rfHIJk9iQ8Ud7h0X".toCharArray());
System.out.println("random = " + result);
}
}
The example of our program result are:
random = 2377316738997278218735578855577798247976319451877477254850564896
random = wwqbLxlRynIdptAoxuSIfbABRoOFHLyKaFEscrUoBQjAHPOkcIqfHuMnhXVzaLCf
random = 6nQ0PJf<#VgIi='&d`*`Ru#i_exMoHXb
random = ZGbelXYEiLrlLBXKroCIDJPrevZPIGkD
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!