🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the StringBuilder delete() example method in Java programming and how to use it with an example.
1. StringBuilder delete() example Method Overview
Definition:
The delete() method of the StringBuilder class is used to remove a subsequence of characters from the current sequence.
Syntax:
stringBuilder.delete(start, end)
Parameters:
- start: The beginning index, inclusive.
- end: The ending index, exclusive.
Key Points:
- The indices are 0-based, which means the first character in the sequence is at index 0.
- If start is equal to end, no changes are made.
- If end is greater than the length of the current sequence, end is adjusted to the end of the sequence.
- It will throw StringIndexOutOfBoundsException if start is negative, greater than length(), or greater than end.
2. StringBuilder delete() example Method Example
public class StringBuilderDeleteExample {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("Hello, wonderful World!");
// Deleting "wonderful "
builder.delete(7, 17);
System.out.println(builder); // Outputs: Hello, World!
// Deleting the last character
builder.delete(builder.length() - 1, builder.length());
System.out.println(builder); // Outputs: Hello, World
// Trying to delete with start > end, throws StringIndexOutOfBoundsException
try {
builder.delete(5, 3);
} catch(StringIndexOutOfBoundsException e) {
System.out.println("Caught an exception: " + e.getMessage());
}
}
}
Output:
Hello, World! Hello, World Caught an exception: start > end
Explanation:
In the example:
1. We start with a StringBuilder object containing the text "Hello, wonderful World!".
2. We use the delete() method to remove the substring "wonderful " which lies between indices 7 (inclusive) and 17 (exclusive).
3. We then remove the last character using delete(), demonstrating that you can use the length() method for range calculations.
4. Lastly, we purposely provide a start index greater than the end index to demonstrate the StringIndexOutOfBoundsException.
The delete() method provides a convenient way to remove substrings from a StringBuilder object, with the flexibility to specify any start and end index. This method, like other StringBuilder methods, operates in place and does not create new objects, ensuring efficiency in string manipulation operations.
Related Java StringBuilder class method examples
- Java StringBuilder append() example
- Java StringBuilder delete() example
- Java StringBuilder insert() example
- Java StringBuilder reverse() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment