StringBuilder replace() in Java with Examples (original) (raw)

Last Updated : 18 Apr, 2023

The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end – 1 or to the end of the sequence if no such character exists. At first, the characters of substring are removed and the string passed as parameters are inserted in place of those characters.

Syntax:

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

Parameters: This method accepts three parameters:

  1. start – Integer type value which refers to the starting index.
  2. end – Integer type value which refers to the ending index.
  3. str – String type value which refer to the String that will replace previous contents.

Returns: This method returns StringBuilder object after successful replacement of characters.

Exception: If the start is negative, greater than length(), or greater than end then StringIndexOutOfBoundsException.

Below programs illustrate the java.lang.StringBuilder.replace() method:

Example 1:

Java

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder str

`` = new StringBuilder("WelcomeGeeks");

`` System.out.println("String = "

`` + str.toString());

`` StringBuilder strReturn = str.replace( 1 , 7 , "e are ");

`` System.out.println("After Replace() String = "

`` + strReturn.toString());

`` }

}

Output:

String = WelcomeGeeks After Replace() String = We are Geeks

Example 2:

Java

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder str

`` = new StringBuilder("Tony Stark will die");

`` System.out.println("String = "

`` + str.toString());

`` StringBuilder strReturn = str.replace( 15 , 16 , " not ");

`` System.out.println("After Replace() String = "

`` + strReturn.toString());

`` }

}

Output:

String = Tony Stark will die After Replace() String = Tony Stark will not die

Example 3: When negative index is passed:

Java

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder str

`` = new StringBuilder("Tony Stark");

`` try {

`` StringBuilder strReturn = str.replace(- 15 , 16 , "Captain America");

`` }

`` catch (Exception e) {

`` e.printStackTrace();

`` }

`` }

}

Output:

java.lang.StringIndexOutOfBoundsException: String index out of range: -15 at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:851) at java.lang.StringBuilder.replace(StringBuilder.java:262) at GFG.main(File.java:17)

Example 4: When start index passed is greater than end index:

Java

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder str

`` = new StringBuilder("Tony Stark");

`` try {

`` StringBuilder strReturn = str.replace( 5 , 3 , "Captain America");

`` }

`` catch (Exception e) {

`` e.printStackTrace();

`` }

`` }

}

Output:

java.lang.StringIndexOutOfBoundsException: start > end at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:855) at java.lang.StringBuilder.replace(StringBuilder.java:262) at GFG.main(File.java:17)

References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#replace(int, int, java.lang.String)