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
Post a Comment