by Dinesh Thakur Category: array

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);
          }
}

StringBuffer Insert() in Java Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles