delete(int start, int end) and deleteCharAt(int index)
You can delete characters within a StringBuffer by using the methods delete( ) and deleteCharAt( ).
StringBuffer delete(int start, int end)
- Removes the characters in a substring of this sequence.
StringBuffer deleteCharAt(int index)
- Removes the char at the specified position in this sequence.
public class Main {
public static void main(String[] argv) {
StringBuffer sb = new StringBuffer();
sb.append(true);
sb.append("java2s.com");
sb.delete(1, 2);
System.out.println(sb.toString());
}
}
The output:
tuejava2s.com
Here is a program that demonstrates the delete( ) and deleteCharAt( ) methods:
public class Main {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.delete(4, 7);
System.out.println("After delete: " + sb);
sb.deleteCharAt(0);
System.out.println("After deleteCharAt: " + sb);
}
}
The following output is produced:
After delete: This a test.
After deleteCharAt: his a test.
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