gpt4 book ai didi

java - 删除特殊字符前后的空格

转载 作者:行者123 更新时间:2023-12-01 06:46:19 27 4
gpt4 key购买 nike

我有一个字符串,

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+(?=[),])", "");

即删除紧跟在 (?=...) 任何以下字符 [),] 后面的所有空格 \s+。双反斜杠用于转义。有关正则表达式的更多信息可以在这里查看:

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

关于java - 删除特殊字符前后的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com