Pei*_*udo 3 java replace space
我有这样的例子:
"hello . world . thanks ."
Run Code Online (Sandbox Code Playgroud)
我想得到这个:
"hello. world. thanks."
Run Code Online (Sandbox Code Playgroud)
我试过了
text = text.replaceAll(" .",".");
text = text.replaceAll(" \\.",".");
text = text.replaceAll(" \\.","\\.");
Run Code Online (Sandbox Code Playgroud)
但它不起作用.任何解决方案
谢谢大家
replaceAll使用正则表达式进行第一次辩论.如果你想要一些非常简单的东西(也就是给出的例子,假设你的实际问题不是很复杂),你可以使用replace.
text = text.replace(" .", ".");
Run Code Online (Sandbox Code Playgroud)
你的第二种方法应该有效.
我想你之前可能有1到多个空格或标签..在这种情况下你需要使用+量词来匹配1到多个空格,如果你有它们
text = text.replaceAll("\\s+[.]",".");
Run Code Online (Sandbox Code Playgroud)
由于.在正则表达式中具有特殊含义,因此您需要使用\\.或者按字面意思[.]对待它.
\s 类似于 [ \t\r\n]