In this article, we will discuss StringBuffer’s delete() method which deletes sub-string starting from specified start index-position to end index-position
1. StringBuffer’s delete() method:
- This method deletes portion of the invoking StringBuffer
- That’s substring starting from specified start index-position till end-1 index-position
1.1 Method Signature:
public StringBuffer delete(int start, int end);
1.2 Returns:
- Returns resulting StringBuffer object after deleting portion of string or substring
- Substring starts from specified start index-position and ends at specified end index-position
- Note: Start index-position is inclusive and end index-position exclusive
1.3 Throws:
- StringIndexOutOfBoundsException, if index value passed falls out of range i.e.;
- if either start-index or end-index is negative (<0)
- if start-index is greater than end-index
- if end-index is greater than length()
2. Examples on delete() method:
- To delete sub-string from invoking StringBuffer object for the specified range
StringBufferDeleteMethod.java
package in.bench.resources.stringbuffer.methods;
public class StringBufferDeleteMethod {
public static void main(String[] args) {
// StringBuffer - 1
StringBuffer sb1 = new StringBuffer("BenchResources.Net");
// removes character from start-index to end-index
sb1.delete(5, 14);
// print to console
System.out.println("1. removing StringBuffer"
+ " content from 5-14 : " + sb1);
// StringBuffer - 2
StringBuffer sb2 = new StringBuffer("BenchResources.Net");
// to clear all contents of StringBuffer
sb2.delete(0, sb2.length());
// print to console
System.out.println("2. after clearing StringBuffer"
+ " contents : " + sb2);
}
}
Output:
1. removing StringBuffer content from 5-14 : Bench.Net
2. after clearing StringBuffer contents :
Related Articles:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!