How to trim a java stringbuilder?

前端 未结 8 2099
南方客
南方客 2021-01-11 11:18

I have a StringBuilder object that needs to be trimmed (i.e. all whitespace chars /u0020 and below removed from either end).

I can\'t seem to find a method in string

8条回答
  •  臣服心动
    2021-01-11 11:37

    I had exactly your question at first, however, after 5-minute's second thought, I realized actually you never need to trim the StringBuffer! You only need to trim the string you append into the StringBuffer.

    If you want to trim an initial StringBuffer, you can do this:

    StringBuffer sb = new StringBuffer(initialStr.trim());
    

    If you want to trim StringBuffer on-the-fly, you can do this during append:

    Sb.append(addOnStr.trim());
    

提交回复
热议问题