如何在Java中清除或清空StringBuilder。
您可以将setLength设置为0或创建一个新StringBuilder()
实例。请参阅下面的示例-
示例
public class Tester { public static void main(String[] args) { StringBuilder builder = new StringBuilder(); builder.append("sample"); System.out.println(builder.toString()); builder.setLength(0); System.out.println(builder.toString()); builder.append("sample"); System.out.println(builder.toString()); } }
输出结果
sample sample