Trim leading or trailing characters from a string?

后端 未结 6 1599
鱼传尺愫
鱼传尺愫 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 22:11

    You could use a simple iteration if you want to remove the leading characters from a string :

    String removeLeadingChar(String s, char c) {
        int i;
        for(i = 0; i < s.length() && s.charAt(i) == c; ++i);
        return s.substring(i);
    }
    

    same logic applies if you want to remove any trailing char.

提交回复
热议问题