Replace all occurrences of a substring in a String in Java
This post will discuss how to replace all occurrences of a substring in a String in Java.
To replace all occurrences of a substring in a string in Java, we can use one of the following methods:
1. Using replace() or replaceAll() method
The standard solution to replace all occurrences of a substring with another string is using the built-in method replace() of the String class. It replaces each substring in the string that matches the target string with the specified replacement string. For example, to replace all occurrences of "AB" with an empty string "" in a string, we can use:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = str.replace(target, replacement); System.out.println(str); // CD } } |
Alternatively, we can use the replaceAll() method of the String class, which takes a regular expression as the first argument and a replacement string as the second argument. This method will replace all matches of the given pattern with the replacement string and return a new string with the replacements made. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = str.replaceAll(target, replacement); System.out.println(str); // CD } } |
Note that an invocation of the replaceAll() method of the form str.replaceAll(target, replacement) yields exactly the same result as the expression Pattern.compile(target).matcher(str).replaceAll(replacement).
2. Using Matcher.replaceAll() method
We can also directly call the Matcher.replaceAll() method to replace each matching substring with the replacement string. The idea is to use the Pattern.compile() method to create a Pattern object by compiling the regular expression that matches the substring we want to replace. Then, call the Pattern.matcher() method to create a Matcher object by applying the Pattern object to the string we want to modify. Finally, use the Matcher.replaceAll() method to replace every occurrence of the substring with the replacement string. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.regex.Pattern; public class Main { public static String replace(String str, String target, String replacement) { return Pattern.compile(target).matcher(str).replaceAll(replacement); } public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = replace(str, target, replacement); System.out.println(str); // CD } } |
3. Using Apache Commons Lang
If project uses the Apache Commons Lang library, consider using the replace() method from the StringUtils class to replace all occurrences of a string within another string. This method is null-safe and can handle any string input. The usage of the method is as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = StringUtils.replace(str, target, replacement); System.out.println(str); // CD } } |
4. Using replaceFirst() method
The replaceFirst() method of the String class takes a regular expression as the first argument and a replacement string as the second argument. This method will replace only the first match of the given pattern with the replacement string. To replace all occurrences of a substring in a string, we can repeatedly invoke this method within a loop until the resultant string is not the same as the specified string. Here’s an example of this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Main { private static String replace(String str, String target, String replacement) { String prev = null; while (!str.equals(prev)) { prev = str; str = str.replaceFirst(target, replacement); } return str; } public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = replace(str, target, replacement); System.out.println(str); // CD } } |
5. Using indexOf() and substring() methods
Finally, we can use a loop and the indexOf() and substring() methods of the String class to find the position and extract a part of a string. The idea is to use the indexOf() method to find the index of the next occurrence of the substring, then use substring() to create a new string with the replaced substring. This process is repeated again from the last index in a loop until we’re done. For example, to replace all occurrences of "AB" with an empty string "" in a string, we can use:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Main { private static String replace(String str, String target, String replacement) { int index = str.indexOf(target); while (index != -1) { str = str.substring(0, index) + replacement + str.substring(index + target.length()); index = str.indexOf(target, index + replacement.length()); } return str; } public static void main(String[] args) { String str = "ABCABD"; String target = "AB"; String replacement = ""; str = replace(str, target, replacement); System.out.println(str); // CD } } |
That’s all about replacing all occurrences of a substring in a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)