➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
StringUtils replaceEach() Example | In this blog we will discuss the replaceEach() method of StringUtils class. This method is used to replace each of the characters present in the string.
The question might arise what is the difference between replace() and replaceEach() method? The replace() method replaces all the occurrences of the given pattern provided the pattern should be in the same order as in the string and replace accepts only one string whereas replaceEach() accepts the array of strings. Also replaceEach() can replace the occurrence of the given pattern provided it does not need to be the same as in the string.
StringUtils replaceEach() Method Syntax
The syntax for the replaceEach() method is as follows:-
public static String replaceEach(final String text, final String[ ] searchList, final String[ ] replacementList)
Parameters:
text: the original string.
searchList: array of strings that contains the pattern to be replaced.
replacementList: array of the strings that have the string to be placed at a particular position.
Java StringUtils replaceEach() Example
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String str = "7896";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "789" }, new String[] { "156" }));
str = " ";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "789" }, new String[] { "123" }));
}
}
Output:-
The string after the replacement is: 1566
The string after the replacement is:
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String str = "abc";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { " " }, new String[] { " " }));
str = "abc";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "789" }, new String[] {}));
}
}
Output:-
The string after the replacement is: abc
The string after the replacement is: abc
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String str = "abc";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "a" }, new String[] { " " }));
str = "abc";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "789" }, new String[] { "123" }));
str = "abcde";
System.out.println("The string after the replacement is: "
+ StringUtils.replaceEach(str, new String[] { "a", "d" }, new String[] { "A", "D" }));
}
}
Output:-
The string after the replacement is: bc
The string after the replacement is: abc
The string after the replacement is: AbcDe
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!