I'm using a StringBuilder in a loop and every x iterations I want to empty it and start with an empty StringBuilder, but I can't see any method similar to the .NET StringBuilder.Clear in the documentation, just the delete method which seems overly complicated.
So what is the best way to clean out a StringBuilder in Java?
解决方案
Two ways that work:
Use stringBuilderObj.setLength(0).
Allocate a new one with new StringBuilder() instead of clearing the buffer.
我正在使用 StringBuilder 在一个循环和每个x迭代中我想要清空它并以空的 StringBuilder 开头,但是我看不到任何类似于.NET的方法 StringBuilder.Clear 在文档中,只是删除看起来过于复杂的方法。
那么在Java中清除 StringBuilder 的最佳方法是什么?
解决方案
两种工作方式:
使用 stringBuilderObj.setLength(0)。
使用分配一个新的StringBuilder()而不是清除缓冲区。