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:-
com.github.javafaker javafaker 1.0.2
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.
package FakerClassExamples; import com.github.javafaker.Faker; public class NumerifyExample { public static void main(String[] args) { Faker faker = new Faker(); System.out.println(faker.numerify("Amod##Mahajan")); System.out.println(faker.numerify("A#mod##Maha#jan")); System.out.println(faker.numerify("A#mod##Mahajan453")); } }
Output
Amod45Mahajan A4mod23Maha2jan A6mod39Mahajan453
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)
package FakerClassExamples; import com.github.javafaker.Faker; public class LetterifyExample { public static void main(String[] args) { Faker faker = new Faker(); System.out.println(faker.letterify("167??789")); System.out.println(faker.letterify("?34?45??")); System.out.println(faker.letterify("Amod??Maha?an")); } }
Output
167cn789 s34r45tv AmodvnMahaban
Method bothify()
String bothify(String string)
Applies both a numerify(String)
and a letterify(String)
over the incoming string.
package FakerClassExamples; import com.github.javafaker.Faker; public class BothifyExample { public static void main(String[] args) { Faker faker = new Faker(); System.out.println(faker.bothify("Amod##167??789")); System.out.println(faker.bothify("?34?45??")); System.out.println(faker.bothify("Amod##Maha?an")); } }
Output
Amod18167mi789 c34t45bh Amod59Mahacan
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.
Thank you , was able to complete this simply