The Wayback Machine - https://web.archive.org/web/20240726033623/https://www.geeksforgeeks.org/stringbuffer-replace-method-in-java-with-examples/
Open In App

StringBuffer replace() Method in Java with Examples

Last Updated : 13 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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)

 



Similar Reads

StringBuffer deleteCharAt() Method in Java with Examples
The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of integer type which refers to the index of the char
2 min read
StringBuffer appendCodePoint() Method in Java with Examples
appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an integer value been assigned. --> appendCodePoint(
3 min read
StringBuffer delete() Method in Java with Examples
The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_point, int end_point) Parameters : The method acce
3 min read
StringBuffer reverse() Method in Java with Examples
The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the StringBuffer after reversing the characters. Examples
2 min read
StringBuffer append() Method in Java with Examples
Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boolean a) is an inbuilt method in Java which is used t
13 min read
StringBuffer codePointCount() method in Java with Examples
The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of the text range and endIndex is index after the la
3 min read
StringBuffer codePointBefore() method in Java with Examples
The codePointBefore() method of StringBuffer class is a method used to take an index as a parameter and returns the “Unicode number” of the character present before that index. The value of index must lie between 0 to length-1. If the char value at (index – 1) is in the low-surrogate range, char at (index – 2) is not negative with value is in the h
2 min read
StringBuffer trimToSize() method in Java with Examples
The trimToSize() method of StringBuffer class is the inbuilt method used to trims the capacity used for the character sequence of StringBuffer object. If the buffer used by StringBuffer object is larger than necessary to hold its current sequence of characters, then this method is called to resize the StringBuffer object for converting this object
2 min read
StringBuffer toString() method in Java with Examples
The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Subsequent changes to this sequence contained by Obje
2 min read
StringBuffer codePointAt() method in Java with Examples
The codePointAt() method of StringBuffer class returns a character Unicode point at that index in sequence contained by StringBuffer. This method returns the “Unicodenumber” of the character at that index. Value of index must be lie between 0 to length-1. If the char value present at the given index lies in the high-surrogate range, the following i
2 min read
three90RightbarBannerImg