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 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);
    }
}
Output:

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);
    }
}
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 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);
    }
}
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)



lets make it a lil simple

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.