为了显示效果更好,我们可以将多余的字符去掉,代码如下:
/** *去掉字符串前后的指定字符 */ public static String trimBothChars(String str, String splitter) { String regex = "^" + splitter + "*|" + splitter + "*$"; return str.replaceAll(regex, ""); }
效果:
oldStr:,this is txt, newStr:this is txt