Clearing a string buffer/builder after loop

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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

回答1:

One option is to use the delete method as follows:

StringBuffer sb = new StringBuffer(); for (int n = 0; n 

Another option (bit cleaner) uses setLength(int len):

sb.setLength(0); 

See Javadoc for more info:



回答2:

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 


回答3:

You have two options:

Either use:

sb.setLength(0);  // It will just discard the previous data, which will be garbage collected later.   

Or use:

sb.delete(0, sb.length());  // A bit slower as it is used to delete sub sequence.   

NOTE

Avoid declaring StringBuffer or StringBuilder objects within the loop else it will create new objects with each iteration. Creating of objects requires system resources, space and also takes time. So for long run, avoid declaring them within a loop if possible.



回答4:

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


回答5:

I suggest creating a new StringBuffer (or even better, StringBuilder) for each iteration. The performance difference is really negligible, but your code will be shorter and simpler.



回答6:

public void clear(StringBuilder s) {     s.setLength(0); } 

Usage:

StringBuilder v = new StringBuilder(); clear(v); 

for readability, I think this is the best solution.



回答7:

StringBuffer sb = new SringBuffer(); // do something wiht it sb = new StringBuffer(); 

i think this code is faster.



回答8:

Already good answer there. Just add a benchmark result for StringBuffer and StringBuild performance difference use new instance in loop or use setLength(0) in loop.

The summary is: In a large loop

  • StringBuilder is much faster than StringBuffer
  • Create new StringBuilder instance in loop have no difference with setLength(0). (setLength(0) have very very very tiny advantage than create new instance.)
  • StringBuffer is slower than StringBuilder by create new instance in loop
  • setLength(0) of StringBuffer is extremely slower than create new instance in loop.

Very simple benchmark (I just manually changed the code and do different test ):

public class StringBuilderSpeed { public static final char ch[] = new char[]{'a','b','c','d','e','f','g','h','i'};  public static void main(String a[]){     int loopTime = 99999999;     long startTime = System.currentTimeMillis();     StringBuilder sb = new StringBuilder();     for(int i = 0 ; i 

}

New StringBuilder instance in loop: Time cost: 3693, 3862, 3624, 3742

StringBuilder setLength: Time cost: 3465, 3421, 3557, 3408

New StringBuffer instance in loop: Time cost: 8327, 8324, 8284

StringBuffer setLength Time cost: 22878, 23017, 22894

Again StringBuilder setLength to ensure not my labtop got some issue to use such long for StringBuffer setLength :-) Time cost: 3448



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!