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
Java Book
Essential Classes
StringBuffer:
- Create StringBuffer object
- append
- StringBuffer capacity()
- charAt(int index): get the char at specified index
- delete(int start, int end) and deleteCharAt(int index)
- ensureCapacity( )
- getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- indexOf(String str)
- lastIndexOf(String str)
- StringBuffer length()
- Insert(): add data in the middle of a StringBuffer
- replace():replace a StringBuffer
- StringBuffer reverse()
- setCharAt(int index, char ch)
- setLength
- substring
- toString():Convert StringBuffer to String