Java StringBuffer delete substring or char from String

Introduction

To delete characters within a StringBuffer use the methods delete() and deleteCharAt().

These methods are shown here:

StringBuffer delete(int startIndex, int endIndex)  
StringBuffer deleteCharAt(int loc) 

Here is a program that demonstrates the delete() and deleteCharAt() methods:


// Demonstrate delete() and deleteCharAt()
public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test.");

    sb.delete(4, 7);//from   w ww  .java 2  s  .co m
    System.out.println("After delete: " + sb);

    sb.deleteCharAt(0);
    System.out.println("After deleteCharAt: " + sb);
  }
}



PreviousNext

Related