java - Specify exact StringBuilder capacity or use default? -


i have method in have input url string , have remove suffix , add another. reason use stringbuilder, wondering have specify exact capacity of builder or rely on default value 16 not enough in of cases, capacity expand in code always.?

my code this:

private string constructstring(final string url, final string suffixtoremove, final string suffixtoadd) {         stringbuilder result = new stringbuilder(url.length() - suffixtoremove.length() + suffixtoadd.length());         .....     } 

i want determine if faster put default capacity of builder or put exact capacity constructed getting .length of 3 strings. string.length slow operation?

you can this:

final int len = url.length() - suffixtoremove.length(); final stringbuilder sb = new stringbuilder(url); sb.setlength(len); return sb.append(suffixtoadd).tostring(); 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -