加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在逗号,句点和其他标点符号后插入空格

发布时间:2020-12-15 04:43:34 所属栏目:Java 来源:网络整理
导读:在 Java中,在一些标点符号之后修复丢失的空格的最佳方法是: ,. ; : ? ! 例如: String example = "This is!just an:example,of a string,that needs?to be fixed.by inserting:a whitespace;after punctuation marks."; 输出应该是: "This is! just an: ex
在 Java中,在一些标点符号之后修复丢失的空格的最佳方法是:

,. ; : ? !

例如:

String example = "This is!just an:example,of a string,that needs?to be fixed.by inserting:a whitespace;after punctuation marks.";

输出应该是:

"This is! just an: example,that needs? to be fixed. by inserting: a whitespace; after punctuation marks."

很明显,这不起作用:

example = example.replaceAll("[,.!?;:]"," ");

所以我正在寻找一个等待你帮助的解决方案.
谢谢!!

解决方法

你必须在你的替换表达式中添加$0,你可以使用:

example = example.replaceAll("[,"$0 ");

它将使用该内容和空格替换匹配的正则表达式.

顺便说一句,如果你想确保你没有多个空格,你可以这样做:

example = example.replaceAll("[,"$0 ").replaceAll("s+"," ");

将转换:

This is!just an:example,that needs?to be fixed.by
inserting:a whitespace;after punctuation marks.;

至:

This is! just an: example,that needs? to be fixed. by inserting: a whitespace; after punctuation marks.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读