StringBuffer insert() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   6086 Share
☰ Table of Contents

Table of Content:


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 . . .