random string java

By | May 6, 2020

In this tutorial, you are going to learn how to generate a random string in java. We are using randomAlphabetic method present in RandomStringUtils class of Apache to generate a random string java.

apache.commons.lang3.RandomStringUtils.randomAlphabetic:

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

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Syntax:

RandomStringUtils.randomAlphabetic(int count)

Parameters:

  • count the length of random string to create

Returns:

  • the random string

Examples:

Output: It prints a random 5 char string for example WvnJP. Random characters are from a-z to A-Z.

 

Random number generation:

randomNumeric method is used to create a random number in java. It creates a random string whose length is the number of characters specified. Characters will be chosen from the set of numeric characters.

Syntax:

RandomStringUtils.randomNumeric(int count)

Example:

Output: It prints a 4 digit number in the form of a string as we have given the length of random string to create as 4.

Ex: 7746

Recommended Posts:

StringUtils methods in java

Binary search using Arrays class

 

Tags: random string java

Leave a Reply

Your email address will not be published. Required fields are marked *