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

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

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

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 09:37

    Might be a bit cheap, but you might also use these without any other library:

    final var string = "   Hello World   ";
    
    final var rtrimmed = ("." + string).trim().substring(1);
    // => "   Hello World"
    
    var ltrimmed = (string + ".").trim();
    ltrimmed = ltrimmed.substring(0, ltrimmed.length - 1);
    // => "Hello World   "
    

    I admit, that the ltrimmed is not that pretty, but it will do the trick.

提交回复
热议问题