Java 删除特殊字符前后的空格

Java 删除特殊字符前后的空格,java,string,Java,String,我有一根绳子 String sample = "He won the International Rating Chess Tournament (IRCT ) which concluded on Dec. 22 at the Blue Sky Hotel , Canada"; 我想删除字符“'”和“,”之前的空格(如果有)。所以最终的输出应该是 He won the International Rating Chess Tournament (IRCT) which concluded o

我有一根绳子

String sample = "He won the International Rating Chess Tournament (IRCT ) which concluded on Dec. 22 at the Blue Sky Hotel , Canada";
我想删除字符“'”和“,”之前的空格(如果有)。所以最终的输出应该是

He won the International Rating Chess Tournament (IRCT) which concluded on Dec. 22 at the Blue Sky Hotel, Canada
谁能告诉我怎么做

sample.replaceAll("\\s+(?=[),])", "");
i、 e.删除紧跟在
(?=…)
后面的所有空格
\s+
,]。双反斜杠用于逃跑。有关正则表达式的更多信息,请参见此处:

i、 e.删除紧跟在
(?=…)
后面的所有空格
\s+
,]。双反斜杠用于逃跑。有关正则表达式的更多信息,请参见此处:

如果要删除
之前的任意数量的空格,可以使用正则表达式:

sample = sample.replaceAll("\\s+,", ",").replaceAll("\\s+\\)", ")");
如果要删除
之前的任意数量的空格,可以使用正则表达式:

sample = sample.replaceAll("\\s+,", ",").replaceAll("\\s+\\)", ")");
将所有“)”替换为“)”,重复应用,直到没有任何内容被替换。将所有“)”替换为“)”,重复应用,直到没有任何内容被替换。
    String sample = "He won the International Rating Chess Tournament (IRCT ) which concluded on Dec. 22 at the Blue Sky Hotel , Canada";
    sample=sample.replace(" )", ")");
    sample=sample.replace(" ,", ",");