How can I clear or empty a StringBuilder?

This question already has an answer here:

  • Clearing a string buffer/builder after loop 8 answers

  • Two ways that work:

  • Use stringBuilderObj.setLength(0) .
  • Allocate a new one with new StringBuilder() instead of clearing the buffer.

  • There are basically two alternatives, using setLength(0) to reset the StringBuilder or creating a new one in each iteration. Both can have pros and cons depending on the usage.

    If you know the expected capacity of the StringBuilder beforehand, creating a new one each time should be just as fast as setting a new length. It will also help the garbage collector, since each StringBuilder will be relatively short-lived and the gc is optimized for that.

    When you don't know the capacity, reusing the same StringBuilder might be faster. Each time you exceed the capacity when appending, a new backing array has to be allocated and the previous content has to be copied. By reusing the same StringBuilder, it will reach the needed capacity after some iterations and there won't be any copying thereafter.


    delete is not overly complicated :

    myStringBuilder.delete(0, myStringBuilder.length());
    

    You can also do :

    myStringBuilder.setLength(0);
    
    链接地址: http://www.djcxy.com/p/12550.html

    上一篇: LEA指令?

    下一篇: 如何清除或清空StringBuilder?