I found in a existing Java program code the following snip:
StringBuffer b = new Stringbuffer()
// fill the StringBuffer
(...)
String temp = b.toString();
return temp.subSequence(0, b.toString().length() - 1);
I don't understand why they done a subSequence from position 0 to the end...
Is the format of a StringBuilder after the convertition into a String that ugly?
They, they wrote this code aren't in my company anymore, so I can't ask them...
Hope you guys can help me out.
The idea is to strip one char from the end, note that all Java standard library classes follow a convention about start / end index parameters, star is inclusive and end is exclusive. Also I agree that the code is ugly, it should be
...
return b.subSequence(0, b.length() - 1);
it's shorter and also more efficient. Also StringBuffer is a legacy class you can replace it with faster StringBuilder
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