Java StringBuilder replace() Method

Java StringBuilder replace() Method | The replace() method of the StringBuilder class of the Java programming language is used to replace the characters or the substring in the given StringBuilder object. The substring or the character begins at the particular index and ends at the particular index according to the user input or the parameters we supply to the replace() method.

The syntax for the StringBuilder replace() method is as follows:-
public StringBuilder replace(int begin, int end, String string)

begin: the start index from the replacement should begin.
end: the end index where the replacement should end.
string: the string which replaces the substring.
Return type: StringBuilder
Returns: the string after replacement
Exception: Throws IndexOutOfBoundException if the passed parameters are out of bound.

Examples for the replace method are as follows:-
String: “Hey People”
Replace: “Hey”
Replace by: “Hi”
The string after replacement: “Hi People”

String: “Good Afternoon”
Replace: “Afternoon”
Replace with: “Morning”
The string after replacement: “Good Morning”

Replace StringBuilder Java Example

In this program, we are passing the string to the constructor of StringBuilder and then calling replace() method, and then printing the same.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Life is too short...!");
      System.out.println("StringBuilder = " + sb);
      StringBuilder sb1 = sb.replace(1, 4, "IFE");
      System.out.println("After replacement = " + sb1);


StringBuilder = Life is too short…!
After replacement = LIFE is too short…!

Java StringBuilder replace() Method Example

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Tiger is our National Animal");
      System.out.println("StringBuilder = " + sb);
      StringBuilder sb1 = sb.replace(13, 21, " NATIONAL ");
      System.out.println("After replacement = " + sb1);


StringBuilder = Tiger is our National Animal
After replacement = Tiger is our NATIONAL Animal

StringBuilder Replace Java Exception

This code will show you the exception thrown by the replace() method. The replace() method throws the StringIndexOutOfBoundsException when the parameters passed are out of bound.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Hello, World!");
      try {
         StringBuilder sb1 = sb.replace(-15, 16, "Program");
      } catch (Exception e) {


java.lang.StringIndexOutOfBoundsException: start -15, end 13, length 13
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(
at java.base/java.lang.AbstractStringBuilder.replace(
at java.base/java.lang.StringBuilder.replace(
at Main.main(

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *