Make Selenium Easy

Javafaker API – numerify(), letterify() and bothify() methods of Faker Class

Introduction

Method names in the title might confuse you but those are the best words to give them when you go through this post.

If you want to create a string in a fixed format like “Amod123Mahajan” or “amod1991mahajan@gmail.com” etc then the above methods will be useful for you.

Maven Dependency

I have used below Javafaker API dependency:-

Prerequisite posts

I have covered Javafaker API below posts. You can go through it to understand more about Javafaker API.

Javafaker API – Generate Real-Time Fake Data – Faker Class

Generate Random Numbers Using Javafaker API – Java

Method numerify()

String numerify​(String numberString)

As per official Javadoc, the above method returns a string with the ‘#‘ characters in the parameter replaced with random digits between 0-9 inclusive.

For example, the string “ABC##EFG” could be replaced with a string like “ABC99EFG“.

In simple words, if you want to have a number/s at specific places of a string just put a hash(“#”) there and numerify() method will put a random number there.

Output

Method letterify()

String letterify​(String letterString)

As per official Javadoc, the above method returns a string with the ‘?’ characters in the parameter replaced with random alphabetic characters.

For example, the string “12??34” could be replaced with a string like “12ab34”.

In simple words, if you want to have an alphabet/s at specific places of a string just put a hash(“?”) there and letterify() method will put a random alphabet there.

By default, it will put lower case letters. If you want it should put upper case letters then you can use below-overloaded method.

public java.lang.String letterify​(java.lang.String letterString, boolean isUpper)

Output

Method bothify()

String bothify​(String string)

Applies both a numerify(String) and a letterify(String) over the incoming string.

Output

These methods will be helpful to create random email ids or alphanumeric strings etc.

You can clone above Git Repo from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

1 thought on “Javafaker API – numerify(), letterify() and bothify() methods of Faker Class

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Javafaker API – numerify(), letterify() and bothify() methods of Faker Class

by Amod Mahajan time to read: 2 min
1
%d bloggers like this: