Home »
Java programming language
Java StringBuffer replace(int spos, int epos , String s) method with Example
Java StringBuffer replace(int spos, int epos , String s) method: Here, we are going to learn about the replace(int spos, int epos , String s) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on July 02, 2019
StringBuffer Class replace(int spos, int epos , String s)
- This method is available in package java.lang.StringBuffer.replace(int spos , int epos , String s).
- This method is used to replace the characters in a substring with the specified string in the method and substring starts from spos(Starting position) and ends with epos(Ending position).
- The procedure of this method is first to delete all the characters in a substring and then start inserting specified string from starting position(spos) defined in a method.
Syntax:
StringBuffer replace(int spos , int epos , String s){
}
Parameter(s):
We pass three one object in the method of the StringBuffer i.e. spos(starting position), epos(ending position) and String s. The substring is in between spos and epos and the specified string will be inserted in between epos and spos after removing all the characters in a substring.
Return value:
The return type of this method is StringBuffer that means this method returns a reference of this object.
Java program to demonstrate example of replace() method
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(" welcome in java world ");
// use replace(int spos , int epos , String s)
// it will replace all the characters between
// spos and epos with the given string in the method.
// Display result after implementing replace
// (12,16,"C++")i.e. java substring will be
// replaced by the string C++
System.out.println("The result will be after implementing method is :" + sb.replace(12, 16, "C++"));
}
}
Output
D:\Programs>javac StringBufferClass.java
D:\Programs>java StringBufferClass
The result will be after implementing method is : welcome in C++ world