[text] - using RandomStringGenerator for alpha numeric

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[text] - using RandomStringGenerator for alpha numeric

Greg Huber
​Hello,

How would I use org.apache.commons.text.RandomStringGenerator to replace
the RandomStringUtils alpha numeric number?

String text = RandomStringUtils.randomAlphanumeric(num);

The docs only show how to use a-z :

RandomStringGenerator generator = new
                RandomStringGenerator.Builder()
                .withinRange('a', 'z').build();

String text = generator.generate(num);

Cheers


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [text] - using RandomStringGenerator for alpha numeric

Amey Jadiye
Hi Greg,

Thanks for writing to us, as range are the codePoints you can use it like
below.

RandomStringGenerator generator = new
                RandomStringGenerator.Builder()
                .withinRange('0', 'z').build();

However that will include all the numbers, capital letters, small letters
and some special characters. between (48 to 112)

We are including feature in next release so you can give characters you
want to generate random string, but that will be available in Text 1.2
release.

For now as an alternate you can use below code as well passing it array of
a to z and 0 to 9.

Set<Integer> characters = Stream.of('a', 'b', '1', '2').map(i -> (int)
i).collect(Collectors.toSet());
String str = new RandomStringGenerator.Builder().filteredBy(i ->
characters.contains(i)).build().generate(5);

Let us know if you need more info.

Regards,
Amey

On Tue, Jun 27, 2017 at 8:03 PM, Greg Huber <[hidden email]> wrote:

> ​Hello,
>
> How would I use org.apache.commons.text.RandomStringGenerator to replace
> the RandomStringUtils alpha numeric number?
>
> String text = RandomStringUtils.randomAlphanumeric(num);
>
> The docs only show how to use a-z :
>
> RandomStringGenerator generator = new
>                 RandomStringGenerator.Builder()
>                 .withinRange('a', 'z').build();
>
> String text = generator.generate(num);
>
> Cheers
>
> ​
>



--

---------------------------------------------------------------------

To unsubscribe, e-mail: [hidden email]

For additional commands, e-mail: [hidden email]
Loading...