2014-09-25 159 views
-1

我想要做的是将多个空间替换为一个字符串中的单个空间。如何将多个空格替换为字符串(java)中的单个空格?

我有字符串变量

String text = "i want to replace multiple space"; 

,我希望它变成

String text = "i want to replace multiple space"; 

我该怎么办呢?,之前请帮助我,谢谢。

+0

你在哪里卡住了? – 2014-09-25 14:02:34

+0

这是回答[这里](http://stackoverflow.com/questions/3958955/how-to-remove-duplicate-white-spaces-in-string-using-java) – Jose 2014-09-25 14:03:49

回答

1

用正则表达式 尝试

"my extra spaced string".replaceAll("\\s+?"," "); 

这就是preatty它。

0

使用此:

text = text.replaceAll("\\s{2,}", " "); 
+0

以及如何替换文本文件中的多个空间逐行? – 2014-09-25 14:33:25

+0

你可能想看看http://stackoverflow.com/questions/20039980/java-replace-line-in-text-file,然后用这个问题的答案做替换。 – jarz 2014-09-26 13:59:09

相关问题