Java StringBuilder replace() Method



The Java StringBuilder replace() method is used to replace the characters in a substring of a StringBuilder object with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence, if no such character exists.

The replace() method accepts three parameters as an integer and string that holds the values of the start, end, and str. It throws an exception if the start index value is negative or greater than the string length.

Syntax

Following is the syntax of the Java StringBuilder replace() method −

public StringBuilder replace(int start, int end, String str)

Parameters

  • start − This is the beginning index, inclusive.

  • end − This is the ending index, exclusive.

  • str − This is the String that will replace previous contents.

Return Value

This method returns this object.

Example

If the given start index value is positive, and less than the string length, the replace() method replaces the character in the specified string.

In the following program, we are instantiating the StringBuilder class with the value “Java Programming”. Then, using the replace() method, we are trying to replace the character of the sub-string “Language” of this sequence at the startIndex 5, and endIndex 16.

package com.tutorialspoint.StringBuilder;
public class Replace {
   public static void main(String[] args) {
      
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("Java Programming");
      System.out.println("The given String is: " + sb);
      
      //initialize the startIndex, endIndex, and string value
      int startIndex = 5;
      int endIndex = 16;
      String str = "Language";
      System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
      System.out.println("The given sub-string is: " + str);
      
      //using the replace() method
      System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
   }
}

Output

On executing the above program, it will produce the following result −

The given String is: Java Programming
The given startIndex and endIndex values are: 5 and 16
The given sub-string is: Language
After replace the string: Java Language

Example

If the given startIndex value is negative, the replace() method throws IndexOutOfBoundException.

In the following example, we are creating an object of the StringBuilder class with the value “TutorialsPoint”. Using the replace() method, we are trying to replace the characters of the sub-string “Point” of this sequence at the startIndex -1 and endIndex 10.

package com.tutorialspoint.StringBuilder;
public class Replace {
   public static void main(String[] args) {
      try {
         
         //create an object StringBuilder class
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         System.out.println("The given String is: " + sb);
         
         //initialize the startIndex, endIndex, and string value
         int startIndex = -1;
         int endIndex = 10;
         String str = "India";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given String is: TutorialsPoint
The given startIndex and endIndex values are: -1 and 10
The given sub-string is: India
java.lang.StringIndexOutOfBoundsException: start -1, end 10, length 14
	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:307)
	at com.tutorialspoint.StringBuilder.Replace.main(Replace.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: start -1, end 10, length 14

Example

If the given start index value is greater than the string length, this method throws the IndexOutOfBoundException.

In this example, we are instantiating the StringBuilder class with the value “hello”. Using the replace() method, we are trying to replace the characters of the sub-string “llo” of this sequence at the startIndex 10 and endIndex 5.

package com.tutorialspoint.StringBuilder;
public class Replace {
   public static void main(String[] args) {
      try {
         
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("hello");
         System.out.println("The given String is: " + sb);
         
         //initialize the startIndex, endIndex, and string value
         int startIndex = 10;
         int endIndex = 5;
         String str = "world";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Above program produces the following output −

The given String is: hello
The given startIndex and endIndex values are: 10 and 5
The given sub-string is: world
java.lang.StringIndexOutOfBoundsException: start 10, end 5, length 5
	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:307)
	at com.tutorialspoint.StringBuilder.Replace.main(Replace.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: start 10, end 5, length 5
java_lang_stringbuilder.htm
Advertisements