Java.lang.StringBuffer.deleteCharAt() Method

Advertisements


Description

The java.lang.StringBuffer.deleteCharAt() method removes the char at the specified position in this sequence. This sequence is shortened by one char.

Declaration

Following is the declaration for java.lang.StringBuffer.deleteCharAt() method

public StringBuffer deleteCharAt(int index)

Parameters

  • index -- This is the Index of char to remove.

Return Value

This method returns this object.

Exception

  • StringIndexOutOfBoundsException -- if the index is negative or greater than or equal to length().

Example

The following example shows the usage of java.lang.StringBuffer.deleteCharAt() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("Java lang package");
    System.out.println("buffer = " + buff);
 
    // deleting character from index 4 to index 9
    buff.delete(4, 9);
    System.out.println("After deletion = " + buff);

    buff = new StringBuffer("amit");
    System.out.println("buffer = " + buff);
    // deleting character at index 2
    buff.deleteCharAt(2);
    System.out.println("After deletion = " + buff);
  }
}

Let us compile and run the above program, this will produce the following result:

buffer = Java lang package
After deletion = Java package
buffer = amit
After deletion = amt



Advertisements
Advertisements