How to Clear / Empty Content of StringBuffer in Java


In this example, we will learn hoe to remove the contents of a StringBuffer.

Source Code

package com.beginner.examples;

public class ClearStringBufferExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//create StringBuffer
		StringBuilder sBuilder=new StringBuilder();
		//Add content
		sBuilder.append("I");
		
		sBuilder.append("Love");
		
		sBuilder.append("You");
		
		System.out.println(sBuilder);
		
		//clear content Method length() Is the number of characters
		sBuilder.delete(0, sBuilder.capacity());

		System.out.println(sBuilder);
		
	}

}

Output:

ILoveYou
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments