How to Insert a Value into StringBuffer in Java


In this example we will show the common method to insert a value to StringBuffer object.

Source Code

package com.beginner.examples;

public class StringBufferInsert {

	public static void main(String[] args) {
		
		//Create StringBuffe.
		StringBuffer stringBuffer = new StringBuffer("Hello World");
		
		System.out.println("Before inserting elements , StringBuffer is : " + stringBuffer);
		
		/*
		 * To insert a value into StringBuffer use the method insert() of Java StringBuffer.
		 */
		stringBuffer.insert(11, " !");
		
		System.out.println("After inserting elements , StringBuffer is : " + stringBuffer);

	}

}

Output:

Before inserting elements , StringBuffer is : Hello World
After inserting elements , StringBuffer is : Hello World !
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments