BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuilder delete()

Last Updated: October 4, 2022 by Chaitanya Singh | Filed Under: java

Java StringBuilder delete() method is used to delete a portion of the string. A StringBuilder instance represents a character sequence. We can delete a portion of this char sequence, by specifying start and end index in delete() method.

The syntax of delete() method is:

//deletes a substring from first char till 5th char
sb.delete(0, 5); //end index is exclusive

//deletes a substring from 2nd char till 4th char
sb.delete(1, 4);

Here, sb is an object of StringBuilder class.

delete() Description

public StringBuilder delete(int start, int end): It deletes a substring from this StringBuilder instance from start index till end index.

This method throws StringOutOfBoundsException, if any of the following condition occurs:

  • If indexes are negative.
  • If any of the index is greater than or equal to the length.
  • if start index > end index.

delete() Parameters

The delete() method of Java StringBuilder class takes a two parameters:

  • start: It represents the starting index, deletion starts from here. It is inclusive.
  • end: It represents the end index, deletion end here. It is exclusive.

delete() Return Value

  • It returns a new StringBuilder object containing the resultant string (char sequence ) after deletion.

Example 1: Deleting a Substring

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("BeginnersBook");
    System.out.println("String: "+sb);

    //deleting the substring "Beginners"
    sb = sb.delete(0, 9);
    System.out.println("String after deletion: "+sb);
  }
}

Output:

Java StringBuilder delete() Output_1

Example 2: When both start and end index are equal

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Text");
    System.out.println("String: "+sb);

    //start and end index are equal
    //nothing will happen as start index is inclusive and
    //end index is exclusive 
    sb = sb.delete(2, 2);
    System.out.println("String after deletion: "+sb);
  }
}

Output:

Java StringBuilder delete() Output_2

Example 3: Removing first and last character from String

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Text");
    System.out.println("String: "+sb);

    //removing first character
    sb = sb.delete(0, 1);

    //removing last character
    sb = sb.delete(sb.length()-1, sb.length());

    System.out.println("String after deletion: "+sb);
  }
}

Output:

Java StringBuilder delete() Output_3

Example 4: When start index is greater than end index

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Text");
    System.out.println("String: "+sb);
    //start index is greater than end index
    sb = sb.delete(2, 1);
    System.out.println("String after deletion: "+sb);
  }
}

Output:

Java StringBuilder delete() Output_4

Recommended Posts

  • Java StringBuilder trimToSize()
  • Java StringBuilder lastIndexOf()
  • Java StringBuilder indexOf()
  • Java StringBuilder ensureCapacity()

Top Related Articles:

  1. Java StringBuilder trimToSize()
  2. how to copy one hashmap content to another hashmap
  3. Java String startsWith() Method with examples
  4. Java StringBuilder Class With Examples
  5. Java StringBuffer insert()

Tags: Java-StringBuilder

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap