Java: StringBuffer insert() method

In this article, we will discuss StringBuffer’s insert() method which is used to insert specified argument at the specified offset (or say starting from specified index-position)

 

insert() method:

  • This method is used to insert given argument to the buffer at the specified offset (or starting from specified index-position)
  • before inserting, internally convert respective data-type into String format
  • Note: There are 12 variants or overloaded insert() method, those are,

Method Signature:

public StringBuffer insert(int offset, boolean b);

public StringBuffer insert(int offset, char c);

public StringBuffer insert(int offset, char[] str);

public StringBuffer insert(int index, char[] str, 
		int offset, int len);

public StringBuffer insert(int dstOffset, CharSequence s);

public StringBuffer insert(int dstOffset, CharSequence s, 
		int start, int end);

public StringBuffer insert(int offset, double d);

public StringBuffer insert(int offset, float f);

public StringBuffer insert(int offset, int i);

public StringBuffer insert(int offset, long l);

public StringBuffer insert(int offset, Object obj);

public StringBuffer insert(int offset, String str);

Returns:

  • Returns StringBuffer object after inserting given argument at the specified offset
  • Argument can be any primitive data-types like boolean, char, int, long, float, double, etc.
  • It can also be used to insert char[] array, CharSequence, Object or String

Throws:

  • IndexOutOfBoundsException, if offset value passed invalid
  • StringIndexOutOfBoundsException, if offset value passed invalid

 

Examples on insert() method:

1. To insert specified primitive boolean data-type

  • insert boolean-value to StringBuffer object at the specified offset or specified index-position

Method signature:

public StringBuilder insert(int offset, boolean b);

StringBufferInsertMethodForBoolean.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForBoolean {

	public static void main(String[] args) {

		// define boolean variable
		boolean boolTest1 = true;
		boolean boolTest2 = false;

		// StringBuffer - 1  
		StringBuffer sb1 = new StringBuffer("BenchResources.Net");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 18th index insert(offset, boolean) method
		sb1.insert(18, boolTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 18th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = new StringBuffer(" BenchResources.Net");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 0th index insert(offset, boolean) method
		sb2.insert(0, boolTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 0th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : BenchResources.Net
1.B After inserting at 18th postion : BenchResources.Nettrue


2.A Before inserting  :  BenchResources.Net
2.B After inserting at 0th postion : false BenchResources.Net

 

2. To insert specified primitive int data-type

  • insert int-value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuilder insert(int offset, int i);

StringBufferInsertMethodForInteger.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForInteger {

	public static void main(String[] args) {

		// define int variable
		int iTest1 = 19;
		int iTest2 = 333;

		// StringBuffer - 1
		StringBuffer sb1 = new StringBuffer("BenchResources.Net");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 5th index using insert(offset, int) method
		sb1.insert(5, iTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 5th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2= new StringBuffer(" BenchResources.Net");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 15th index using insert(offset, int) method
		sb2.insert(15, iTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 15th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : BenchResources.Net
1.B After inserting at 5th postion : Bench19Resources.Net


2.A Before inserting  :  BenchResources.Net
2.B After inserting at 15th postion :  BenchResources333.Net

 

3. To insert specified primitive long data-type

  • insert long-value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, long l);

StringBufferInsertMethodForLong.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForLong {

	public static void main(String[] args) {

		// define long variable
		long lngTest1 = 1912345;
		long lngTest2 = 333444999;

		// StringBuffer - 1
		StringBuffer sb1 = new StringBuffer("BenchResources.Net");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 5th index using insert(offset, long) method
		sb1.insert(5, lngTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 5th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2= new StringBuffer(" BenchResources.Net");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 15th index using insert(offset, long) method
		sb2.insert(15, lngTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 15th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : BenchResources.Net
1.B After inserting at 5th postion : Bench1912345Resources.Net


2.A Before inserting  :  BenchResources.Net
2.B After inserting at 15th postion :  BenchResources333444999.Net

 

4. To insert specified primitive float data-type

  • insert float-value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, float f);

StringBufferInsertMethodForFloat.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForFloat {

	public static void main(String[] args) {

		// define float variable
		float ftTest1 = 19.23456f;
		float ftTest2 = 258.589635f;

		// StringBuffer - 1
		StringBuffer sb1 = 
				new StringBuffer("Hello it's float value of ");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 26th index using insert(offset, float) method
		sb1.insert(sb1.length(), ftTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 26th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2= new StringBuffer(" is a float value");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 0th index using insert(offset, float) method
		sb2.insert(0, ftTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 0th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : 
	Hello it's float value of 
1.B After inserting at 26th postion : 
	Hello it's float value of 19.23456


2.A Before inserting  :
	is a float value
2.B After inserting at 0th postion :
	258.58963 is a float value

 

5. To insert specified primitive double data-type

  • insert double-value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, double d);

StringBufferInsertMethodForDouble.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForDouble {

	public static void main(String[] args) {

		// define double variable
		double dblTest1 = 19.23456;
		double dblTest2 = 258.589635;

		// StringBuffer - 1
		StringBuffer sb1 = 
				new StringBuffer("Hello it's double value of ");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 26th index using insert(offset,double) method
		sb1.insert(sb1.length(), dblTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 26th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = new StringBuffer(" is a double value.");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 0th index using insert(offset, double) method
		sb2.insert(0, dblTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 0th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : 
	Hello it's double value of 
1.B After inserting at 26th postion : 
	Hello it's double value of 19.23456


2.A Before inserting  :
	is a double value.
2.B After inserting at 0th postion :
	258.589635 is a double value.

 

6. To insert specified primitive char data-type

  • insert char-value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, char c);

StringBufferInsertMethodForChar.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForChar {

	public static void main(String[] args) {

		// define char variable
		char chTest1 = 'A';
		char chTest2 = 'b';

		// StringBuffer - 1  
		StringBuffer sb1 = 
				new StringBuffer("The captial character is ");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 25th index insert(offset, char) method
		sb1.insert(25, chTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 25th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = 
				new StringBuffer(" is a lower-case character.");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 0th index insert(offset, char) method
		sb2.insert(0, chTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 0th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : The captial character is 
1.B After inserting at 25th postion : The captial character is A


2.A Before inserting  :  is a lower-case character.
2.B After inserting at 0th postion : b is a lower-case character.

 

7. To insert specified char[] array

  • insert char-array to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, char[] str);

public StringBuffer insert(int index, char[] str, 
		int offset, int len);

StringBufferInsertMethodForCharArray.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForCharArray {

	public static void main(String[] args) {

		// define char[] variable
		char[] chArrayTest1 = {'B', 'e', 'n', 'c', 'h'};
		char[] chArrayTest2 = {'R', 'e', 
				's', 'o', 'u', 'r', 'c', 'e', 's'};

		// StringBuffer - 1  
		StringBuffer sb1 = new StringBuffer(
				"Implicitly converted char[] to SB ");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 34th index insert(offset, char[]) method
		sb1.insert(sb1.length(), chArrayTest1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 34th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = new StringBuffer(
				" is a convreted SB from char[] array.");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 0th index insert(offset, char[]) method
		sb2.insert(0, chArrayTest2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 0th postion : " + sb2);
	}
}

Output:

1.A Before inserting  :
	Implicitly converted char[] to SB 
1.B After inserting at 34th postion :
	Implicitly converted char[] to SB Bench


2.A Before inserting  :
	is a convreted SB from char[] array.
2.B After inserting at 0th postion :
	Resources is a convreted SB from char[] array.

 

8. To insert specified CharSequence

  • insert CharSequence to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int dstOffset, CharSequence s);

public StringBuffer insert(int dstOffset, CharSequence s, 
		int start, int end);

StringBufferInsertMethodForCharSequence.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForCharSequence {

	public static void main(String[] args) {

		// define CharSequence variable
		CharSequence charSeq1 = "Bench";
		CharSequence charSeq2 = "Resources.Net";

		// StringBuffer - 1  
		StringBuffer sb1 = new StringBuffer("Resources.Net");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 0th index insert(offset, CharSequence) method
		sb1.insert(0, charSeq1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 0th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = new StringBuffer("Bench");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 5th index insert(offset, CharSequence) method
		sb2.insert(sb2.length(), charSeq2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 5th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : Resources.Net
1.B After inserting at 0th postion : BenchResources.Net


2.A Before inserting  : Bench
2.B After inserting at 5th postion : BenchResources.Net

 

9. To insert specified String object/value

  • insert String object/value to StringBuffer object at the specified offset or index-position

Method signature:

public StringBuffer insert(int offset, Object obj);

public StringBuffer insert(int offset, String str);

StringBufferInsertMethodForStringObject.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferInsertMethodForStringObject {

	public static void main(String[] args) {

		// define String variable
		String str1 = "Bench";
		String str2 = "Tutorial.tut";

		// StringBuffer - 1  
		StringBuffer sb1 = new StringBuffer("Resources.Net");

		// 1. A : before inserting - 
		// internally invokes toString() method
		System.out.println("1.A Before inserting  : " + sb1);

		// insert at 0th index insert(offset, CharSequence) method
		sb1.insert(0, str1);

		// 1. B : after inserting - 
		// internally invokes toString() method
		System.out.println("1.B After inserting"
				+ " at 0th postion : " + sb1);


		// StringBuffer - 2
		StringBuffer sb2 = new StringBuffer("Online");

		// 2. A : before inserting - 
		// internally invokes toString() method
		System.out.println("\n\n2.A Before inserting  : " + sb2);

		// insert at 6th index insert(offset, CharSequence) method
		sb2.insert(sb2.length(), str2);

		// 2. B : after inserting - 
		// internally invokes toString() method
		System.out.println("2.B After inserting"
				+ " at 6th postion : " + sb2);
	}
}

Output:

1.A Before inserting  : Resources.Net
1.B After inserting at 0th postion : BenchResources.Net


2.A Before inserting  : Online
2.B After inserting at 6th postion : OnlineTutorial.tut

 

References:

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer lastIndexOf() method
Java: StringBuffer indexOf() method