What is a good alternative of LTRIM and RTRIM in Java?

后端 未结 7 908
既然无缘
既然无缘 2020-12-01 09:16

What is a good alternative of JavaScript ltrim() and rtrim() functions in Java?

7条回答
  •  半阙折子戏
    2020-12-01 09:42

    Based on the answer of @bezmax I had a look at the Spring StringUtils, but it couldn't justify the overload for me of switching to the Spring framework. Therfore I decided to create a Characters class to easily left / right trim strings for any given character(-class). The class is available on GitHub.

    Characters.valueOf('x').trim( ... )
    Characters.valueOf('x').leftTrim( ... )
    Characters.valueOf('x').rightTrim( ... )
    

    If you would like to trim for whitespaces, there is a predefined character-class available:

    Characters.WHITESPACE.trim( ... )
    Characters.WHITESPACE.leftTrim( ... )
    Characters.WHITESPACE.rightTrim( ... )
    

    The class does feature also other kinds of character operations, such condense, replace or split.

提交回复
热议问题