StringBuffer类insert(int offset,String s) (StringBuffer Class insert(int offset , String s))
This method is available in package java.lang.StringBuffer.insert(int offset, String s).
软件包java.lang.StringBuffer.insert(int offset,String s)中提供了此方法。
This method is used to insert the string representation with the given object at the specified position.
此方法用于在指定位置插入具有给定对象的字符串表示形式。
This method is overridable so it is available in different-2 forms like:
此方法是可重写的,因此有两种不同的形式可用,例如:
- StringBuffer insert(int offset , boolean b)
- StringBuffer insert(int offset , char c)
- StringBuffer insert(int offset , char[] c) etc
Syntax:
句法:
StringBuffer insert(int offset , String s){
}
Parameter(s):
参数:
We can pass two or three objects according to our need as a parameter in the method and that object will insert in a sequence at the specified position.
我们可以根据需要传递两个或三个对象作为方法中的参数,并且该对象将按顺序插入到指定位置。
Return value:
返回值:
The return type of this method is StringBuffer that means this method returns a reference to this object.
该方法的返回类型为StringBuffer ,这意味着该方法返回对该对象的引用。
Java程序演示insert()方法的示例 (Java program to demonstrate example of insert() method)
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java is a programming language : ");
// use insert(int offset ,Boolean b) it will insert the Boolean parameter
// at index 33 as string to the StringBuffer
sb.insert(33, true);
// Display result after inserting
System.out.println("The result will be after inserting Boolean object to the StringBuffer is :" + sb.toString());
sb = new StringBuffer("Version of Java is : ");
// use insert(int offset , int i) it will insert the Integer parameter
// at index 21 as string to the StringBuffer
sb.insert(21, 8);
// Display result after inserting
System.out.println("The result will be after inserting Integer object to the StringBuffer is :" + sb.toString());
}
}
Output
输出量
D:\Programs>javac StringBufferClass.java
D:\Programs>java StringBufferClass
The result will be after inserting Boolean object to the StringBuffer is :Java is a programming language : true
The result will be after inserting Integer object to the StringBuffer is :Version of Java is : 8
翻译自: https://www.includehelp.com/java/stringbuffer-insert-int-offset-string-s-method-with-example.aspx