Java: StringBuffer & Concatenation

后端 未结 6 2092
梦如初夏
梦如初夏 2021-01-05 01:59

I\'m using StringBuffer in Java to concat strings together, like so:

StringBuffer str = new StringBuffer();

str.append(\"string value\");

6条回答
  •  清歌不尽
    2021-01-05 02:42

    Another possibility is that StringBuilder objects return themselves when you call append, meaning you can do:

    str.append("string value").append(" ");
    

    Not quite as slick, but it is probably an easier solution than the + " " method.

    Another possibility is to build a wrapper class, like PaddedStringBuilder, that provides the same methods but applies the padding you want, since you can't inherit.

提交回复
热议问题