In this article, we will discuss how to clear or delete StringBuffer contents
1. Clearing StringBuffer contents:
Here,
- Start-index for the contents i.e.; 0
- end-index which lasts till its length
- needs to be specified as delete(start, end) method arguments
1.1 To clear or delete all StringBuffer contents:
Method Signature:
public synchronized StringBuffer delete(int start, int end);
Syntax:
sbuffer.delete(0, sbuffer.length());
ClearStringBufferContents.java
package in.bench.resources.sb.operation;
public class ClearStringBufferContents {
public static void main(String[] args) {
// Sample StringBuffer
StringBuffer sbuffer = new StringBuffer();
// add some contents
sbuffer.append("Bench");
sbuffer.append("Resources.");
sbuffer.append("Net");
// before clearing sbuffer
System.out.println("Before clear : "
+ sbuffer.toString());
// clear or delete all
sbuffer.delete(0, sbuffer.length());
// after clearing sbuffer
System.out.println("After clear : "
+ sbuffer.toString());
}
}
Output:
Before clear : BenchResources.Net
After clear :
Note: In a very similar way, StringBuilder contents can also be cleared
We will execute a demo program for StringBuilder as well in the below example
1.2 To clear or delete all StringBuilder contents:
Method Signature:
public StringBuilder delete(int start, int end);
Syntax:
sbuilder.delete(0, sbuilder.length());
ClearStringBuilderContents.java
package in.bench.resources.sb.operation;
public class ClearStringBuilderContents {
public static void main(String[] args) {
// Sample StringBuilder
StringBuilder sbuilder = new StringBuilder();
// add some contents
sbuilder.append("World");
sbuilder.append("Health");
sbuilder.append("Organization");
// before clearing sBuilder
System.out.println("Before clear : "
+ sbuilder.toString());
// clear or delete all
sbuilder.delete(0, sbuilder.length());
// after clearing sBuilder
System.out.println("After clear : "
+ sbuilder.toString());
}
}
Output:
Before clear : WorldHealthOrganization
After clear :
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- StringBuffer class
- StringBuffer append() method (13)
- StringBuffer capacity() method
- StringBuffer charAt(int index) method
- StringBuffer delete(int start, int end) method
- StringBuffer deleteCharAt(int index) method
- StringBuffer ensureCapacity(int minimumCapacity) method
- StringBuffer indexOf() method (2)
- StringBuffer insert() method (12)
- StringBuffer lastIndexOf() method (2)
- StringBuffer length() method
- StringBuffer replace(int start, int end, String str) method
- StringBuffer reverse() method
- 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 !!