如何在Java中删除逗号前后的空格

huangapple go评论56阅读模式
英文:

How to remove spaces before and after the comma in java

问题

我遇到了输入不匹配的错误。

使用了正则表达式("//s*,//s*",","),但它不起作用。

英文:

I have an input mismatch error.

used regular expression ("//s*,//s*",",") but it doesn't work.

答案1

得分: 0

你应该使用\\而不是//

String input = "a , b , c , d";
String output = input.replaceAll("\\s*,\\s*", ",");
System.out.println(output); // 输出: "a,b,c,d"
英文:

You should use \\ instead of //

String input = "a , b , c , d";
String output = input.replaceAll("\\s*,\\s*", ",");
System.out.println(output); // output: "a,b,c,d"

huangapple
  • 本文由 发表于 2023年3月3日 20:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定