Table of Contents

    StringBuffer insert() Method in Java: Usage and Examples

    The insert() method inserts one string into another. It is overloaded to accept values of all the simpletypes,plusStrings,Objects,andCharSequences.Likeappend(),itcallsString.valueOf( ) to obtain the string representation of the value it is called with. This string is then inserted into the invoking StringBuffer object. These are a few of its forms:

    Syntax

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

    Program

    // Demonstrate insert().
    class insertDemo {
    	public static void main(String args[]) {
    	StringBuffer strng = new StringBuffer("I Java!");
    	strng.insert(2, "like ");
    	System.out.println(strng);
     }
    }

    Output

    I like Java!
    Press any key to continue . . .