StringBuffer insert (int index, char [] str, int offset, int len) :This method inserts a substring into the StringBuffer object starting at position index. The substring is the string representation of len characters from the str []array starting at index position offset.
For example,
char[] chArr ={‘D’,’i’,’n’,’e’,’s’,’h’,’ ‘};
StringBuffer s2 = new StringBuffer(“Mr Thakur”);
s2.insert(3,chArr,0,7);
As a result, characters from chArr array starting from position 0 will be inserted in the invoking
string buffer at the position 3.So the resulting string buffer will be “ Mr Dinesh Thakur “.
public class StringBufferInsert
{
public static void main(String[] args)
{
StringBuffer s1 = new StringBuffer("Welcome Java");
s1.insert(7," to");
System.out.println("sl : " +s1);
StringBuffer s2 = new StringBuffer("Mr Thakur");
char[] chArr ={'D','i','n','e','s','h',' '};
s2.insert(3,chArr,0,7);
System.out.println("s2 : " + s2);
}
}