Searching...
Tuesday, 8 October 2013

How to empty/clear StringBuffer and StringBuilder ?

10:19

Use StringBuffer delete method and pass start and end index to delete all contents in the StringBuffer.

public StringBuffer delete(int start,int end)

Example

package com.pretech;
public class StringBufferTest {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		sb.append("Hello");
		sb.append("StringBuffer");
		System.out.println(sb);
		sb.delete(0, sb.length());
		System.out.println(sb);
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append("Hello");
		sbuilder.append("Stringbuilder");
		System.out.println(sbuilder);
		sbuilder.delete(0, sbuilder.length());
		System.out.println(sbuilder);
	}
}

Output



HelloStringBuffer

HelloStringbuilder


0 comments: