StringBuffer类replace(int spos,int epos,String s) (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).
软件包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).
此方法用于用方法中的指定字符串替换子字符串中的字符,并且子字符串从spos (起始位置)开始,以epos (结束位置)结束。
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.
此方法的过程是首先删除子字符串中的所有字符,然后从方法中定义的起始位置( spos )开始插入指定的字符串。
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.
我们在StringBuffer方法中传递了三个对象,即spos (开始位置), epos (结束位置)和String s 。 子字符串位于spos和epos之间,并且在删除子字符串中的所有字符后,将在epos和spos之间插入指定的字符串。
Return value:
返回值:
The return type of this method is StringBuffer that means this method returns a reference of this object.
该方法的返回类型为StringBuffer,这意味着该方法返回该对象的引用。
Java程序演示replace()方法的示例 (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