i have a question, what is the best way to append ints and Strings to build a new String? In the allocation debug tool i see too much allocations if i use the operator +.
But i have tried also with StringBuffer and there are still too much allocations.
Anyone can help me?
Thanks
Use StringBuilder
or StringBuffer
but with adequate initial capacity to avoid re-allocation. The default capacity is 16, so once you exceed that, the data has to be copied into a new bigger place. Use append
not +
.
int integer = 5;
StringBuilder s = new StringBuilder(100);
s.append("something");
s.append(integer);
s.append("more text");
Will allocate 100 slots upfront.
Reference: http://developer.android.com/reference/java/lang/StringBuilder.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With