JAVA 去除字符串前后的指定字符

为了显示效果更好,我们可以将多余的字符去掉,代码如下:

/**
*去掉字符串前后的指定字符
*/
public static String trimBothChars(String str, String splitter) {
    String regex = "^" + splitter + "*|" + splitter + "*$";
    return str.replaceAll(regex, "");
}

效果:

oldStr:,this is txt,
newStr:this is txt

  

posted @ 2020-09-29 15:37  ToEden  阅读(3492)  评论(0)    收藏  举报