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 ondelete
feel more neat. - or allocate new 1 instead rather clearing buffer. may not ideal option still option.
Comments
Post a Comment