Java String Handling

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 occurrence of text from StringBuilder/StringBuffer.

How to replace text in StringBuilder or StringBuffer?

Replacing content in StringBuilder or StringBuffer is done using 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 which is exclusive) and actual replacement text.

Java StringBuilder Replace Example:

Ouput

We wanted to replace Two with Five, so we specified 4 as the starting index. Start index is inclusive that means the replacement starts at the specified index. We wanted to replace the whole world i.e. till index 6. However, the end index in the replace method is exclusive, that 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 same for StringBuffer as well.

How to replace all occurrences of characters in StringBuffer or StringBuilder?

Unlike String class, StringBuilder and StringBuffer classes do not provide replaceAll method which replaces all occurrences of character sequence. The replaceAll method of String class internally uses Pattern and Matcher classes to do the replacements. Here is the source code of 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.

Lets create our own replaceAll method for StringBuilder class using the above code.

Output

We created our own version of replaceAll method with 3 parameters, source StringBuilder object, find string and replace string. The replaceAll method of the Matcher class returns String object, so we passed that in the StringBuilder constructor to create new StringBuilder object and returned it.

If you do not want to create 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 works for the StringBuffer class as they have similar APIs.

Please also see how to replace text in String object or how to replace element in ArrayList object. Visit Java StringBuilder and StringBuffer examples to learn more.

References:
StringBuilder JavaDoc
StringBuffer JavaDoc

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.