How to Reset StringBuffer in Java


The example aims to reset StringBuffer object to null or empty value in Java.

Source Code

package com.beginner.examples;

public class StrinBufferReset {

	public static void main(String[] args) {
		
		//create StringBuffer.
		StringBuffer stringBuffer = new StringBuffer("Hello World!");
		System.out.println("Before reset StringBuffer: " + stringBuffer) ;
		/*
		 * Use delete() method of StringBuffer to reset StringBuffer.
		 */
		stringBuffer.delete(0, stringBuffer.length());
		System.out.println("After reset StringBuffer:" + stringBuffer);
	}

}

Output:

Before reset StringBuffer: Hello World!
After reset StringBuffer:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments