Java StringBuilder replace & replace all example shows how to replace content in StringBuilder or StringBuffer object. It also shows to replace all occurrences of text from StringBuilder/StringBuffer.
How to replace text in StringBuilder or StringBuffer?
Replacing content in StringBuilder or StringBuffer is done using the replace
method.
1 |
public StringBuilder replace(int start, int end, String replacement) |
The replace
method accepts three arguments, start index (index from where you want to start the replacement), end index (index where you want to stop the replacement – exclusive), and actual replacement text.
Java StringBuilder Replace Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class JavaStringBuilderReplaceExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("One Two Three Four"); //replace Two with Five sb.replace(4, 7, "Five"); System.out.println(sb); } } |
Output
1 |
One Five Three Four |
We wanted to replace Two with Five, so we specified 4 as the starting index. The start index is inclusive that means the replacement starts at the specified index. We wanted to replace the whole word i.e. till index 6. However, the end index in the replace method is exclusive, which means the replacement ends before the specified end index, so we specified 7 as our end index. Lastly, we specified the replacement string as Five.
StringBuffer replace
method is also identical to this, so the above code works the same for StringBuffer as well.
How to replace all occurrences of characters in StringBuffer or StringBuilder?
Unlike the String class, StringBuilder and StringBuffer classes do not provide replaceAll
method that replaces all occurrences of the character sequence. The replaceAll
method of String class internally uses Pattern and Matcher classes (regex) to do the replacements. Here is the source code of the replaceAll
method of String class obtained from OpenJDK 7.
1 2 3 |
public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } |
As you can see from the above code, the find String is compiled as a pattern first and then the matcher is obtained for the pattern using source String. After that, it uses replaceAll
method of the Matcher class to replace all the occurrences of the string with the provided replacement.
The matcher
method of Pattern class accepts CharSequence which is a parent class of StringBuilder and StringBuffer classes. So we can utilize the same code for them as well.
Let’s create our own replaceAll method for the StringBuilder class using the above code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.regex.Pattern; public class JavaStringBuilderReplaceExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("one two three one two three one two three"); System.out.println(replaceAll(sb, "two", "five")); } public static StringBuilder replaceAll(StringBuilder sb, String find, String replace){ return new StringBuilder(Pattern.compile(find).matcher(sb).replaceAll(replace)); } } |
Output
1 |
one five three one five three one five three |
We created our own version of replaceAll
method with 3 parameters, source StringBuilder object, a string to find, and replace string. The replaceAll
method of the Matcher class returns a String object, so we passed that in the StringBuilder constructor to create a new StringBuilder object and returned it.
If you do not want to create a new StringBuilder object but do all the replacements in the same object itself, you can use below-given code to do the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaStringBuilderReplaceExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("one two three one two three one two three"); replaceAll(sb, "two", "five"); System.out.println(sb); } public static void replaceAll(StringBuilder sb, String find, String replace){ //compile pattern from find string Pattern p = Pattern.compile(find); //create new Matcher from StringBuilder object Matcher matcher = p.matcher(sb); //index of StringBuilder from where search should begin int startIndex = 0; while( matcher.find(startIndex) ){ sb.replace(matcher.start(), matcher.end(), replace); //set next start index as start of the last match + length of replacement startIndex = matcher.start() + replace.length(); } } } |
Output
1 |
one five three one five three one five three |
The above code finds the match and replaces it with the replacement text. Before finding the next match, it increases the start index by adding the replacement text length to it.
Both of the StringBuilder replaceAll examples give above also work for the StringBuffer class as they have similar APIs.
Please also see how to replace text in the String object or how to replace an element in the ArrayList object.
This example is a part of the Java StringBuffer tutorial and Java StringBuilder tutorial.
References:
StringBuilder JavaDoc
StringBuffer JavaDoc