java - Is this a good way of clearing a StringBuilder? -


this question has answer here:

i ask if way of clearing/emptying stringbuilder? wrapped in method , called several times, thread safe? not consume lot of memory? better if created stringbuilder outside of method , in class instead?

private void callme(){     stringbuilder builder = new stringbuilder();     builder.delete(0, builder.length()); }  builder.append("some id");  if (builder.tostring().contains("some other id"))     show("information") else     show("invalid id") 

before post answer assuming using stringbuilder in loop , every few iterations may want empty , start empty stringbuilder. in case can think of 2 approaches:

  • invoke setlength(0) on string builder using. prefer on delete feel more neat.
  • or allocate new 1 instead rather clearing buffer. may not ideal option still option.

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -