Insert(): add data in the middle of a StringBuffer

The insert( ) method inserts one string into another. It is overloaded to accept values of all the simple types, plus Strings, Objects, and CharSequences. It calls String.valueOf( ) to obtain the string representation of the value it is called with.

StringBuffer insert(int offset, boolean b)
Inserts the boolean argument into this sequence.
StringBuffer insert(int offset, char c)
Inserts the char argument into this sequence.
StringBuffer insert(int offset, char[] str)
Inserts the char array argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)
Inserts a subarray of the str array argument into this sequence.
StringBuffer insert(int dstOffset, CharSequence s)
Inserts CharSequence into this sequence.
StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
Inserts a subsequence of the specified CharSequence into this sequence.
StringBuffer insert(int offset, double d)
Inserts the double argument into this sequence.
StringBuffer insert(int offset, float f)
Inserts the float argument into this sequence.
StringBuffer insert(int offset, int i)
Inserts the second int argument into this sequence.
StringBuffer insert(int offset, long l)
Inserts the long argument into this sequence.
StringBuffer insert(int offset, Object obj)
Inserts the Object argument into this character sequence.
StringBuffer insert(int offset, String str)
Inserts str.

public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append("java2s.com");
    sb.append(1.2);
    sb.append('a');
    
    sb.insert(3,"JAVA@S.COM");
    System.out.println(sb.toString());
  }
}

The output:


truJAVA@S.COMejava2s.com1.2a

The following sample program inserts strings:

 
public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("java2s.com");
    sb.insert(2, "like ");
    System.out.println(sb);
  }
}

The output of this example is shown here:


jalike va2s.com

public class Main {
  public static void main(String args[]) {
    Object objectRef = "hello";
    String string = "goodbye";
    char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    boolean booleanValue = true;
    char characterValue = 'K';
    int integerValue = 7;
    long longValue = 10000000;
    float floatValue = 2.5f;
    double doubleValue = 33.3;

    StringBuffer buffer = new StringBuffer();

    buffer.insert(0, objectRef);
    buffer.insert(0, "  ");
    buffer.insert(0, string);
    buffer.insert(0, "  ");
    buffer.insert(0, charArray);
    buffer.insert(0, "  ");
    buffer.insert(0, charArray, 3, 3);
    buffer.insert(0, "  ");
    buffer.insert(0, booleanValue);
    buffer.insert(0, "  ");
    buffer.insert(0, characterValue);
    buffer.insert(0, "  ");
    buffer.insert(0, integerValue);
    buffer.insert(0, "  ");
    buffer.insert(0, longValue);
    buffer.insert(0, "  ");
    buffer.insert(0, floatValue);
    buffer.insert(0, "  ");
    buffer.insert(0, doubleValue);

    System.out.printf("buffer after inserts:\n%s\n\n", buffer.toString());

  }
}
Home 
  Java Book 
    Essential Classes  

StringBuffer:
  1. Create StringBuffer object
  2. append
  3. StringBuffer capacity()
  4. charAt(int index): get the char at specified index
  5. delete(int start, int end) and deleteCharAt(int index)
  6. ensureCapacity( )
  7. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  8. indexOf(String str)
  9. lastIndexOf(String str)
  10. StringBuffer length()
  11. Insert(): add data in the middle of a StringBuffer
  12. replace():replace a StringBuffer
  13. StringBuffer reverse()
  14. setCharAt(int index, char ch)
  15. setLength
  16. substring
  17. toString():Convert StringBuffer to String