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)

Parameters:
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:-
Example-1:
String: “Hey People”
Replace: “Hey”
Replace by: “Hi”
The string after replacement: “Hi People”

Example-2:
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);
   }
}

Output:-

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);
   }
}

Output:-

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");
         System.out.println(sb1);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output:-

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

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 *