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