Home » Java programming language

Java StringBuilder deleteCharAt() method with example

StringBuilder Class deleteCharAt() method: Here, we are going to learn about the deleteCharAt() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019

StringBuilder Class deleteCharAt() method

  • deleteCharAt() method is available in java.lang package.
  • deleteCharAt() method is used to delete the character at the given index.
  • deleteCharAt() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • deleteCharAt() method may throw an exception at the time of assigning index.
    StringIndexOutOfBoundsException - This exception may throw when the given argument value is less than 0 or larger than or similar to length().

Syntax:

    public StringBuilder deleteCharAt(int indices);

Parameter(s):

  • int indices – represents the index of deleting character.

Return value:

The return type of this method is StringBuilder, it returns this StringBuilder object.

Example:

// Java program to demonstrate the example // of StringBuilder deleteCharAt(int indices) // method of StringBuilder public class DeleteCharAt { public static void main(String[] args) { // Creating an StringBuilder object StringBuilder st_b = new StringBuilder("Java World"); // Display before deletion System.out.println("st_b = " + st_b); // By using deleteCharAt(4) method is to delete the character // i.e. whitespace at the given index 4 st_b = st_b.deleteCharAt(4); // Display st_b after deletion System.out.println("st_b.deleteCharAt(4) = " + st_b); } }

Output

st_b = Java World
st_b.deleteCharAt(4) = JavaWorld
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.