Java.lang.StringBuilder.deleteCharAt() Method

Advertisements


Description

The java.lang.StringBuilder.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.StringBuilder.deleteCharAt() method

public StringBuilder 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.StringBuilder.deleteCharAt() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

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

    str = new StringBuilder("amit");
    System.out.println("string = " + str);
    // deleting character at index 2
    str.deleteCharAt(2);
    System.out.println("After deletion = " + str);
  }
}  

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

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



Advertisements
Advertisements