有时候在拼接字符串时候,某个拼接变量为空时,导致最后拼接的字符串首尾出现一个或多个分割符,为了拿掉首尾分割符,可以
使用如下正则方式:
public static String trimBothEndsChars(String srcStr, String splitter) {
String regex = "^" + splitter + "*|" + splitter + "*$";
return srcStr.replaceAll(regex, "");
}
test:
String str =";;3453;435;45,34543;;"
String out = trimBothEndsChars(str, ";")
out:
3453;435;45,34543