StringBuffer insert()


insert()

The insert() method inserts one string into another. It overloads all primitive types of values and accepts more strings, objects, and CharSequences values. Like append (), it acquires a string representation of the value it is called. This string will then be inserted into the invigorating StringBuffer object. It has several forms:

StringBuffer insert(int index, string str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)

Here, the index specifies the index at which point the string will be inserted into the invoking StringBuffer object.

Example

Program
public class Main 
{
  public static void main(String[] args) 
  {
    StringBuffer sb = new StringBuffer("guriasoft");
    
    System.out.println("Before insert = " + sb);
    StringBuffer s = sb.insert(0,"Welcome to ");
    System.out.println("After insert = " + s);
  }
}