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:

Download  Run Code

 
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:

Download  Run Code

 
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:

Download  Run Code

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:

Download Code

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:

Download  Run Code

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:

Download  Run Code

That’s all about replacing all occurrences of a substring in a String in Java.