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  

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