📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
StringBuffer.deleteCharAt()
method in Java is used to remove the character at a specified index within the StringBuffer
object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
deleteCharAt
Method Syntax- Examples
- Deleting a Character at a Specific Index
- Handling Edge Cases
- Conclusion
Introduction
The deleteCharAt()
method is a member of the StringBuffer
class in Java. It allows you to remove a character at a specified position within the StringBuffer
. This is useful when you need to modify the content of a string dynamically by removing specific characters.
deleteCharAt Method Syntax
The syntax for the deleteCharAt
method is as follows:
public synchronized StringBuffer deleteCharAt(int index)
Parameters:
index
- the index of the character to be removed.
Returns:
- The
StringBuffer
object after the specified character has been removed.
Throws:
StringIndexOutOfBoundsException
- if theindex
argument is negative or not less than the length of this sequence.
Examples
Deleting a Character at a Specific Index
The deleteCharAt
method can be used to remove a character at a particular index in a StringBuffer
object.
Example
public class StringBufferDeleteCharAtExample { public static void main(String[] args) { // Create a StringBuffer object with initial content StringBuffer sb = new StringBuffer("Hello, World!"); // Delete the character at index 5 sb.deleteCharAt(5); // Print the modified content of the StringBuffer System.out.println(sb.toString()); } }
Output:
Hello World!
Handling Edge Cases
It is important to handle cases where the specified index is out of bounds.
Example
public class StringBufferDeleteCharAtExample { public static void main(String[] args) { // Create a StringBuffer object with initial content StringBuffer sb = new StringBuffer("Hello"); try { // Attempt to delete the character at an invalid index sb.deleteCharAt(10); } catch (StringIndexOutOfBoundsException e) { // Handle the exception System.out.println("Error: " + e.getMessage()); } try { // Attempt to delete the character at a negative index sb.deleteCharAt(-1); } catch (StringIndexOutOfBoundsException e) { // Handle the exception System.out.println("Error: " + e.getMessage()); } } }
Output:
Error: String index out of range: 10 Error: String index out of range: -1
Conclusion
The StringBuffer.deleteCharAt()
method in Java provides a way to remove a character at a specified index within a StringBuffer
object. By understanding how to use this method, you can easily modify the content of a StringBuffer
dynamically, which is useful for various text manipulation tasks. This method is particularly useful for applications that require the ability to modify strings by removing specific characters.
Comments
Post a Comment
Leave Comment