Skip to content

Java StringBuilder Replace & Replace All Example (StringBuffer)

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.

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:

Output

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.

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.

Output

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.

Output

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

About the author

Leave a Reply

Your email address will not be published.