代码如下:
public static void main(String[] args) {
String exp = "(^[, ]+)|([, ]+$)";
System.out.println(", , ,1,2,3, ".replaceAll(exp, ""));
System.out.println(" ,,,,,, ".replaceAll(exp, ""));
System.out.println(",,,1,2,3, ,, ".replaceAll(exp, ""));
}
运行结果如下:
1,2,3
1,2,3