Java StringBuffer insert() Examples

This page shows the usage of all Java StringBuffer insert() methods with an example.

Java StringBuffer insert() Methods

Java StringBuffer insert() method has 12 overloaded versions:
  1. StringBuffer insert(int offset, boolean b) - Inserts the string representation of the boolean argument into this sequence.
  2. StringBuffer insert(int offset, char c) - Inserts the string representation of the char argument into this sequence.
  3. StringBuffer insert(int offset, char[] str) - Inserts the string representation of the char array argument into this sequence.
  4. StringBuffer insert(int index, char[] str, int offset, int len) - Inserts the string representation of a subarray of the str array argument into this sequence.
  5. StringBuffer insert(int dstOffset, CharSequence s) - Inserts the specified CharSequence into this sequence.
  6. StringBuffer insert(int dstOffset, CharSequence s, int start, int end) - Inserts a subsequence of the specified CharSequence into this sequence.
  7. StringBuffer insert(int offset, double d) - Inserts the string representation of the double argument into this sequence.
  8. StringBuffer insert(int offset, float f) - Inserts the string representation of the float argument into this sequence.
  9. StringBuffer insert(int offset, int i) - Inserts the string representation of the second int argument into this sequence.
  10. StringBuffer insert(int offset, long l) - Inserts the string representation of the long argument into this sequence.
  11. StringBuffer insert(int offset, Object obj) - Inserts the string representation of the Object argument into this character sequence.
  12. StringBuffer insert(int offset, String str) - Inserts the string into this character sequence.

Java StringBuffer insert() Methods Examples

Example: Below simple program demonstrate the usage of all insert() overloaded methods.
public class InsertExample {
 public static void main(String[] args) {
  
  // 12 insert overloaded method
  StringBuffer builder = new StringBuffer("javaguides").insert(1,true);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, 'J');
  System.out.println(builder.toString());
  
  char[] chars = {'d','e','v','e','l','o','p','e','r'};
  builder = new StringBuffer("javaguides").insert(4, chars);
  System.out.println(builder.toString());
  
  CharSequence charSequence = new StringBuilder("J2EE/");
  builder = new StringBuffer("javaguides").insert(0, charSequence);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, 100.0d);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, 100.0f);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, 100);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, 100l);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, new Object());
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, "ultimate");
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, chars, 0, chars.length);
  System.out.println(builder.toString());
  
  builder = new StringBuffer("javaguides").insert(0, charSequence, 0, charSequence.length());
  System.out.println(builder.toString());
  
 }
}
Output:
jtrueavaguides
Jjavaguides
javadeveloperguides
J2EE/javaguides
100.0javaguides
100.0javaguides
100javaguides
100javaguides
java.lang.Object@15db9742javaguides
ultimatejavaguides
developerjavaguides
J2EE/javaguides


Comments