Trim leading or trailing characters from a string?

后端 未结 6 1628
鱼传尺愫
鱼传尺愫 2020-11-27 21:19

How can I trim the leading or trailing characters from a string in java?

For example, the slash character \"/\" - I\'m not interested in spaces, and am looking to t

6条回答
  •  长情又很酷
    2020-11-27 21:51

    You can use Apache StringUtils.stripStart to trim leading characters, or StringUtils.stripEnd to trim trailing characters.

    For example:

    System.out.println(StringUtils.stripStart("//test/me", "/"));
    

    will output:

    test/me

    Note that if for some reason you can't use the whole StringUtils library, you could just rip out the relevant parts, as detailed here:

提交回复
热议问题