Java StringBuffer insert() Method



The Java StringBuffer insert() method is used to insert a string passed argument into a StringBuffer object.

The insert() method accepts different types of an argument, such as Boolean, int, char, and so on. It throws an exception if the given offset is not valid.

The insert() method has 12 polymorphic variants with different parameters: Boolean, char, char[], CharSequnevce, double, float, int, long, object, String, and StringBuffer. (Below are the syntaxes of all the polymorphic variants).

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Syntax

Following is the syntax of the Java StringBuffer insert() method −

public StringBuffer insert(int offset, boolean b)// first syntax 
public StringBuffer insert(int offset, char c)// second syntax
public StringBuffer insert(int offset, char[] str)// third syntax
public StringBuffer insert(int index, char[] str, int offset, int len)// fourth syntax
public StringBuffer insert(int dstOffset, CharSequence s)// fifth syntax
public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)// sixth syntax
public StringBuffer insert(int offset, double d)// seven syntax
public StringBuffer insert(int offset, float f)// eight syntax
public StringBuffer insert(int offset, int i)// nine syntax
public StringBuffer insert(int offset, long l)// ten syntax
public StringBuffer insert(int offset, Object obj)// eleven syntax
public StringBuffer insert(int offset, String str)// twelve syntax 

Parameters

  • offset − This is the offset.

  • b − This is the boolean value.

  • c − This is the char value.

  • strch − This is the character array.

  • index − This is the position at which to insert sub-array.

  • len − This is the number of chars in the sub-array to be inserted.

  • dstoffset − This is the offset.

  • s − This is the sequence to be inserted.

  • start − This is the starting index of the subsequence to be inserted.

  • end − This is the end index of the subsequence to be inserted.

  • d − This is the value of double.

  • f − This is the value of float.

  • i − This is the value of an int.

  • l − This is the value of long.

  • obj − This is the value of an Object.

  • str − This is the value of string.

Return Value

This method returns a reference to this object.

Example

If we pass a Boolean value as an argument to the method, the insert() method appends the Boolean value to the current sequence.

In the following program, we are instantiating the StringBuffer class with the value “Hello”. Then, using the insert() method, we are trying to insert a boolean value “false” to the current sequence at the specified offset 0.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("Hello ");
      System.out.println("The given string is: " + sb);
      //initialize the offset and boolean values
      int offset = 0;
      boolean bool = false;
      System.out.println("The initialize offset and boolean values are: " + offset + " and " + bool);
      //using the insert() method
      System.out.println("After insert boolean value the string is: " + sb.insert(offset, bool));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Hello   
The initialize offset and boolean values are: 0 and false
After insert boolean value the string is: falseHello    

Example

If we pass a double value as an argument to the method, this method inserts the double value into a current sequence.

In the following example, we are creating an object of the StringBuffer class with the value “double “. Using the insert() method, we are trying to insert a double value “34.5d” to the current sequence at the specified offset index 6.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("double");
      System.out.println("The given string is: " + sb);
      //initialize the offset and double values
      int offset = 6;
      double d = 34.5d;
      System.out.println("The initialize offset and double values are: " + offset + " and " + d);
      //using the insert() method
      System.out.println("After insert double value the string is: " + sb.insert(offset, d));
   }
}

Output

Following is the output of the above program −

The given string is: double
The initialize offset and double values are: 6 and 34.5
After insert double value the string is: double34.5

Example

If we pass the int value as an argument to the method, the insert() method inserts the int value into this sequence.

In this program, we are creating a StringBuffer with the value “Integer”. Then, using the insert() method, we are trying to insert the int value “10” into this sequence at the specified offset index 1.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("Integer");
      System.out.println("The given string is: " + sb);
      //initialize the offset and int values
      int offset = 1;
      int i = 10;
      System.out.println("The initialize offset and int values are: " + offset + " and " + i);
      //using the insert() method
      System.out.println("After insert int value the string is: " + sb.insert(offset, i));
   }
}

Output

The above program produces the following output −

The given string is: Integer
The initialize offset and int values are: 1 and 10
After insert int value the string is: I10nteger

Example

If we pass a float value as an argument to the method, this method inserts the float value into the current sequence.

In this example, we are instantiating the StringBuffer class with the value of “float”. Using the insert() method, we are inserting the float value “12.89f” at the specified offset index 3.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("float");
      System.out.println("The given string is: " + sb);
      //initialize the offset and float values
      int offset = 3;
      float f = 12.89f;
      System.out.println("The initialize offset and float values are: " + offset + " and " + f);
      //using the insert() method
      System.out.println("After insert float value the string is: " + sb.insert(offset, f));
   }
}

Output

On executing the above program, it will produce the following output −

The given string is: float
The initialize offset and float values are: 3 and 12.89
After insert float value the string is: flo12.89at

Example

If we pass a long value as an argument to the method, the insert() method inserts the long value of this sequence.

In the following program, we are creating an object of the StringBuffer class with the value “long”. Then, using the insert() method, we are trying to insert the long value “123456” at the specified offset index 1.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("long");
      System.out.println("The given string is: " + sb);
      //initialize the offset and long values
      int offset = 1;
      long l = 123456;
      System.out.println("The initialize offset and long values are: " + offset + " and " + l);
      //using the insert() method
      System.out.println("After insert long value the string is: " + sb.insert(offset, l));
   }
}

Output

Following is the output of the above program −

The given string is: long
The initialize offset and long values are: 1 and 123456
After insert long value the string is: l123456ong

Example

If we pass a string value as an argument to the method, this method inserts the string value into the current sequence.

In this program, we are creating a StringBuffer with the value “Tutorials”. Then, using the insert() method, we are inserting a string value “point” into the current sequence at the specified offset index 9.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {   
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("Tutorials");
      System.out.println("The given string is: " + sb);
      //initialize the offset and string values
      int offset = 9;
      String str = "Point";
      System.out.println("The initialize offset and string values are: " + offset + " and " + str);
      //using the insert() method
      System.out.println("After insert string value the string is: " + sb.insert(offset, str));
   }
}

Output

The above program produces the following output −

The given string is: Tutorials
The initialize offset and string values are: 9 and Point
After insert string value the string is: TutorialsPoint

Example

If we pass an object as an argument to the method, the insert() method inserts the object into the current sequence.

In the following program, we are instantiating the StringBuffer class with the value “Object”. Then, we are, creating an object with the value “this is an ”. Using the insert() method, we are trying to insert the object value into the current sequence at the specified offset index 0.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //instantiate of the StringBuffer class
      StringBuffer sb = new StringBuffer("Object");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char sequence values
      int offset = 0;
      Object obj = "this is an ";
      System.out.println("The initialize offset and object values are: " + offset + " and '" + obj + "'");
      //using the insert() method
      System.out.println("After insert object value, the string is: " + sb.insert(offset, obj));
   }
}

Output

Following is the output of the above program −

The given string is: Object
The initialize offset and object values are: 0 and 'this is an '
After insert object value, the string is: this is an Object

Example

If we pass a char sequence as an argument to the method, this method inserts the char sequence in this sequence.

In this program, we are creating an object of the StringBuffer class with the value “char”. Using the insert() method, we are trying to insert the char sequence value “this is char sequence” into the current sequence at the specified offset index 4.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("char");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char sequence values
      int offset = 4;
      CharSequence cs = "sequence";
      System.out.println("The initialize offset and char sequence are: " + offset + " and " + cs);
      //using the insert() method
      System.out.println("After insert char sequence value the string is: " + sb.insert(offset, cs));
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: char
The initialize offset and char sequence values are: 4 and sequence
After insert char sequence value, the string is: charsequence

Example

If we pass the char sequence, startIndex, and endIndex as an argument to the method, the insert() method inserts the char sequence into the current sequence.

In the following example, we are creating a StringBuffer with the value “Hello”. Then, using the insert() method, we are trying to insert the char sequence “char sequence” value at the offset index 2, with the specified startIndex 0 and endIndex 10.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("Hello");
      System.out.println("The given string is: " + sb);
      //initialize the offset, startIndex, endIndex, and char sequence values
      int offset = 2;
      int startIndex = 0;
      int endIndex = 10;
      CharSequence cs = "char sequence";
      System.out.println("The initialize offset, startIndex, endIndex, and char sequence values are: " + offset + ", " + startIndex +", " + endIndex + ", and '" + cs + "'");
      //using the insert() method
      System.out.println("After insert char sequence value, the string is: " + sb.insert(offset,cs, startIndex, endIndex));
   }
}

Output

On executing the above program, it will produce the following output −

The given string is: Hello
The initialize offset, startIndex, endIndex, and char sequence values are: 2, 0, 8, and 'char sequence'
After insert char sequence value, the string is: Hechar seqllo

Example

If we pass a char value as an argument to the method, this method inserts the char value into this sequence.

In this program, we are creating an object of the StringBuffer class with the value “char”. Then, using the insert() method, we are trying, to insert the char value “A” to this sequence at the specified offset index 4.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("char");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char values
      int offset = 4;
      char ch = 'A';
      System.out.println("The initialize offset and char values are: " + offset + " and " + ch);
      //using the insert() method
      System.out.println("After insert char value, the string is: " + sb.insert(offset,ch));
   }
}

Output

Following is the output of the above program −

The given string is: char
The initialize offset and char values are: 4 and A
After insert char value, the string is: charA

Example

If we pass a char array as an argument to the method, the insert() method inserts the char array into the current sequence.

In the following program, we are instantiating the StringBuffer class with the value “char array”. Then, we are creating char array with the value {‘A’, ’B’, ’C’}. Using the insert() method, we are trying to insert a char array into the current sequence at the specified offset index 0.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("char array");
      System.out.println("The given string is: " + sb);
      //initialize the offset and char array values
      int offset = 0;
      char ch[] = {'A', 'B', 'C'};
      System.out.println("The initialize offset value is: " + offset);
      System.out.print("The char array: ");
      System.out.println(ch);
      //using the insert() method
      System.out.println("After insert char array value, the string is: " + sb.insert(offset,ch));
   }
}

Output

The above program, produces the following output −

The given string is: char array
The initialize offset values is: 0
The char array: ABC
After insert char array value, the string is: ABCchar array

Example

If we pass a char array, index, and length as an argument to the method, this method inserts the char array into this sequence.

In the following program, we are creating a StringBuffer with the value “ABC”. Then, we are creating a char array with the value {‘E’,’F’,’G’,’H’}. Using the insert() method, we are trying to insert the char array value to this sequence at the offset index 0, with the index value 1, and length value 2.

package com.tutorialspoint.StringBuffer;
public class Insert {
   public static void main(String[] args) {
      //create an object the StringBuffer class
      StringBuffer sb = new StringBuffer("ABC");
      System.out.println("The given string is: " + sb);
      //initialize the offset, index, length, and char array values
      int offset = 0;
      int index = 1;
      int length = 2;
      char ch[] = {'D', 'E', 'F', 'H'};
      System.out.println("The initialize offset, index, length values are: " + offset + ", " + index + ", and " + length);
      System.out.print("The char array: ");
      System.out.println(ch);
      //using the insert() method
      System.out.println("After insert char array value, the string is: " + sb.insert(index,ch, offset, length));
   }
}

Output

After executing the above program, it will generate the following output −

The given string is: ABC
The initialize offset, index, length values are: 0, 1, and 2
The char array: DEFH
After insert char array value, the string is: ADEBC
java_lang_stringbuffer.htm
Advertisements