This example shows how to generate a random String in Java.
Generating Random Alphanumeric String in Java.
The code below show you how to use the Apache Commons RandomStringUtils class to generate some random string data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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); } } |
Output:
random = 2377316738997278218735578855577798247976319451877477254850564896
random = wwqbLxlRynIdptAoxuSIfbABRoOFHLyKaFEscrUoBQjAHPOkcIqfHuMnhXVzaLCf
random = 6nQ0PJf<#VgIi=’&d
random = ZGbelXYEiLrlLBXKroCIDJPrevZPIGkD
random = wwqbLxlRynIdptAoxuSIfbABRoOFHLyKaFEscrUoBQjAHPOkcIqfHuMnhXVzaLCf
random = 6nQ0PJf<#VgIi=’&d
*
Ru#i_exMoHXbrandom = ZGbelXYEiLrlLBXKroCIDJPrevZPIGkD