Clearing a string buffer/builder after loop

后端 未结 8 1880
萌比男神i
萌比男神i 2020-12-12 16:39

How do you clear the string buffer in Java after a loop so the next iteration uses a clear string buffer?

8条回答
  •  爱一瞬间的悲伤
    2020-12-12 17:29

    The easiest way to reuse the StringBuffer is to use the method setLength()

    public void setLength(int newLength)

    You may have the case like

    StringBuffer sb = new StringBuffer("HelloWorld");
    // after many iterations and manipulations
    sb.setLength(0);
    // reuse sb
    

提交回复
热议问题