How to trim a java stringbuilder?

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

    You get two strings, but I'd expect the data to be only allocated once. Since Strings in Java are immutable, I'd expect the trim implementation to give you an object that shares the same character data, but with different start- and end indices. At least that's what the substr method does. So, anything you try to optimise this most certainly will have the opposite effect, since you add overhead that is not needed.

    Just step through the trim() method with your debugger.

提交回复
热议问题