Generate Random String in Java using Apache Commons Text
Tags: Apache Commons Apache Commons Text RandomStringGenerator String
Introduction
In this article we show how to generate random String values in Java using the RandomStringGenerator class of Apache Commons Text library. We provide multiple Java code examples with different settings to generate different kinds of data that may be useful for your daily programming scenarios.
Setup Apache Commons Text in Java project
If you are using Gradle build then add the following dependency configuration into build.gradle file.
compile group: 'org.apache.commons', name: 'commons-text', version: '1.9'
Or add the following dependency XML tag to pom.xml file if you are using Maven build.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
Or download commons-text-1.9.jar file from Apache Commons Text download page at commons.apache.org
How to use org.apache.commons.text.RandomStringGenerator
In order to generate a String you need to create an object of RandomStringGenerator class by Builder class and provide settings for output random String. Then invoke the generate() method which provides the length of output random string to generate new random String.
Generate random String with digits, letters and special characters
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample1 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('0', 'z')
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
D2LVJ
Oev@X@CVUk
=JEMbq4zAGAy5aT
Generate random String with digits and letters
import org.apache.commons.text.CharacterPredicates;
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample2 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('0', 'z')
.filteredBy(CharacterPredicates.LETTERS, CharacterPredicates.DIGITS)
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
FkB4D
p4Bq9TmoTP
HKllI0k1xzLZnlf
Generate random String with digits only
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample3 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('0', '9')
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
95697
6933738894
203250212308008
Generate random String with letters only
import org.apache.commons.text.CharacterPredicates;
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample4 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('A', 'z')
.filteredBy(CharacterPredicates.LETTERS)
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
CIHqA
FSJWnVJQMY
IjKJPRiowylevae
Generate random String with lowercase letters only
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample5 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('a', 'z')
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
yadkz
ixbzammknr
ynmngbxboevkrxz
Generate random String with capital letters only
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample6 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.withinRange('A', 'Z')
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
MTPDB
WKGLVJZMEV
MGFPUTINWJBNTIO
Generate random String with with limit list of characters
import org.apache.commons.text.RandomStringGenerator;
public class RandomStringGeneratorExample7 {
public static void main(String[] args) {
RandomStringGenerator generator = new RandomStringGenerator
.Builder()
.selectFrom("12345-".toCharArray())
.build();
String generatedString1 = generator.generate(5);
String generatedString2 = generator.generate(10);
String generatedString3 = generator.generate(15);
System.out.println(generatedString1);
System.out.println(generatedString2);
System.out.println(generatedString3);
}
}
3--33
51--252-21
2-51-1515141555
Happy Coding 😊