Java: StringBuilder insert() method

In this article, we will discuss StringBuilder’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 passed-argument to the buffer at the specified offset (or starting from specified index-position)
  • before inserting, internally converts respective data-type into String format first
  • Note: There are 12 variants or overloaded insert() method, those are,

Method Signature:

public StringBuilder insert(int offset, boolean b);

public StringBuilder insert(int offset, char c);

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

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

public StringBuilder insert(int dstOffset, CharSequence s);

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

public StringBuilder insert(int offset, double d);

public StringBuilder insert(int offset, float f);

public StringBuilder insert(int offset, int i);

public StringBuilder insert(int offset, long l);

public StringBuilder insert(int offset, Object obj);

public StringBuilder insert(int offset, String str);

Returns:

  • Returns StringBuilder 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

 

Examples on insert() method:

1. To insert specified primitive boolean data-type

  • insert boolean-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, boolean b);

StringBuilderInsertMethodForBoolean.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForBoolean {

	public static void main(String[] args) {

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

		// StringBuilder - 1  
		StringBuilder sb1 = new StringBuilder(
				"The boolean value  is inserted");

		// 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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" boolean value is inserted at 0th index");

		// 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  : 
	The boolean value  is inserted
1.B After inserting at 18th postion : 
	The boolean value true is inserted


2.A Before inserting  :  
	boolean value is inserted at 0th index
2.B After inserting at 0th postion : 
	false boolean value is inserted at 0th index

 

2. To insert specified primitive int data-type

  • insert integer-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, int i);

StringBuilderInsertMethodForInteger.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForInteger {

	public static void main(String[] args) {

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

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder(
				"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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" 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 into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, long l);

StringBuilderInsertMethodForLong.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForLong {

	public static void main(String[] args) {

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

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder("Google.com");

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

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

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


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder("Oracle.com");

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

		// insert at 0th index using insert(offset, long) method
		sb2.insert(0, 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  : Google.com
1.B After inserting at 6th postion : Google1912345.com


2.A Before inserting  : Oracle.com
2.B After inserting at 15th postion : 333444999Oracle.com

 

4. To insert specified primitive float data-type

  • insert float-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, float f);

StringBuilderInsertMethodForFloat.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForFloat {

	public static void main(String[] args) {

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

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder(
				"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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" is a float another 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 another value
2.B After inserting at 0th postion : 
	258.58963 is a float another value

 

5. To insert specified primitive double data-type

  • insert double-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, double d);

StringBuilderInsertMethodForDouble.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForDouble {

	public static void main(String[] args) {

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

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder(
				"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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" is double another 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 double another value.
2.B After inserting at 0th postion : 
	258.589635 is double another value.

 

6. To insert specified primitive char data-type

  • insert character-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, char c);

StringBuilderInsertMethodForChar.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForChar {

	public static void main(String[] args) {

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

		// StringBuilder - 1  
		StringBuilder sb1 = new StringBuilder(
				"The upper-case 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(28, chTest1);

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


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" 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 upper-case character is 
1.B After inserting at 25th postion : 
	The upper-case 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 character-array into StringBuilder object at specified offset or index-position

Method signature:

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

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

StringBuilderInsertMethodForCharArray.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForCharArray {

	public static void main(String[] args) {

		// define char[] variable
		char[] chArrayTest1 = {'Y', 'a', 'h', 'o', 'o'};
		char[] chArrayTest2 = {'h', 'o', 't', 
				'm', 'a', 'i', 'l', '.', 'c', 'o', 'm'};

		// StringBuilder - 1  
		StringBuilder sb1 = new StringBuilder(
				"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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(
				" 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 Yahoo


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

 

8. To insert specified CharSequence

  • insert CharSequence-value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int dstOffset, CharSequence s);

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

StringBuilderInsertMethodForCharSequence.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForCharSequence {

	public static void main(String[] args) {

		// define CharSequence variable
		CharSequence charSeq1 = "Make";
		CharSequence charSeq2 = "Travels.enjoy";

		// StringBuilder - 1  
		StringBuilder sb1 = new StringBuilder("MyTrip.com");

		// 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);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder("HoneyMoon");

		// 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  : MyTrip.com
1.B After inserting at 0th postion : MakeMyTrip.com


2.A Before inserting  : HoneyMoon
2.B After inserting at 5th postion : HoneyMoonTravels.enjoy

 

9. To insert specified String object

  • insert String object/value into StringBuilder object at specified offset or index-position

Method signature:

public StringBuilder insert(int offset, Object obj);

public StringBuilder insert(int offset, String str);

StringBuilderInsertMethodForStringObject.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderInsertMethodForStringObject {

	public static void main(String[] args) {

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

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

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

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

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


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

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

		// insert at 6th index insert(offset, String) 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: StringBuilder lastIndexOf() method
Java: StringBuilder indexOf() method