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 StringBuffer delete()

By Chaitanya Singh | Filed Under: java

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

Syntax of delete() method

//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 StringBuffer class.

delete() Description

public StringBuffer delete(int start, int end): It deletes a substring from this StringBuffer 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 StringBuffer 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 StringBuffer object containing the resultant string (char sequence ) after deletion.

Example 1: Deleting a substring

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

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

Output:

Java StringBuffer delete() Output 1

Example 2: If start and end Indexes are same

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    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 StringBuffer delete() Output 2

Example 3: Removing first and last character

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Sample");
    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 StringBuffer delete() Output 3

Example 4: Removing last two characters

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

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

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

Output:

Java StringBuffer delete() Output 4

Example 5: If start index is greater than the end index

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Welcome");
    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 StringBuffer delete() Output 5

Recommended Posts

  • Java StringBuffer insert()
  • Java StringBuffer setLength()
  • Java StringBuffer deleteCharAt()
  • Java StringBuffer appendCodePoint()
❮ Java StringBuffer

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 – 2022 BeginnersBook . Privacy Policy . Sitemap