StringBuffer replace() Method in Java with Examples
Last Updated :
13 Dec, 2021
Improve
The StringBuffer.replace() is the inbuilt method which is used to replace the characters in a substring of this sequence with the characters in the specified String. Here simply the characters in the substring are removed and other char is inserted at the start.
Syntax :
public StringBuffer replace(int first, int last, String st)
Parameters : The method accepts three parameters.
- first : This is of integer type which refers to the starting index.
- last : This is of integer type which refers to the ending index.
- st : This is of string type which refer to the String that will replace previous contents.
Return Value : The method returns this object after performing the above mentioned operations.
Exception : If the first is negative, greater than length(), or greater than last then StringIndexOutOfBoundsException.
Examples :
Input: StringBuffer= "The first planet of solar system is merrhxy" first = 39 last = 42 st = "cur" Output: The first planet of solar system is mercury
Below programs illustrate the java.lang.StringBuffer.replace() method:
Program 1:
java
// Java program to illustrate the // java.lang.StringBuffer.replace() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks"); System.out.println("string buffer = " + sbf); // Replacing substring from index 15 to index 18 sbf.replace( 15 , 18 , " for "); System.out.println("After replacing string buffer= " + sbf); } } |
Output:
string buffer = Welcome to Geekshssgeeks After replacing string buffer= Welcome to Geekforsgeeks
Program 2: When negative index is passed:
java
// Java program to illustrate the // java.lang.StringBuffer.replace() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks"); System.out.println("string buffer = " + sbf); // Replacing substring from index -15 to index -18 sbf.replace(- 15 , - 18 , " for "); System.out.println("After replacing string buffer= " + sbf); } } |
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -15 at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:851) at java.lang.StringBuffer.replace(StringBuffer.java:452) at Geeks.main(Geeks.java:14)
Program 3: When index passed is greater than length
java
// Java program to illustrate the // java.lang.StringBuffer.replace() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks"); System.out.println("string buffer = " + sbf); // Replacing substring from index 215 to index 218 sbf.replace( 215 , 218 , " for "); System.out.println("After replacing string buffer= " + sbf); } } |
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start > length() at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:853) at java.lang.StringBuffer.replace(StringBuffer.java:452) at Geeks.main(Geeks.java:14)