StringBuffer insert() Method in Java: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:42:16   6039  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

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