StringBuffer replace() Method in Java with Examples
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 praogram 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); } } |
string buffer = Welcome to Geekshssgeeks After replacing string buffer= Welcome to Geekforsgeeks
Program 2: When negative index is passed:
// Java praogram 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); } } |
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 praogram 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); } } |
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)
Recommended Posts:
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer setCharAt() method in Java with Examples
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer getChars() method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



