How to trim a java stringbuilder?

前端 未结 8 2095
南方客
南方客 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:29

    only one of you have taken into account that when you convert the String builder to a "string" and then "trim" that you create an immutable object twice that has to be garbage collected, so the total allocation is:

    1. Stringbuilder object
    2. immutable string of the SB object 1 immutable object of the string that has been trimmed.

    So whilst it may "appear" that the trim is faster, in the real world and with a loaded memory scheme it will in fact be worse.

提交回复
热议问题