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

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -