如何与一个空格替换多个空格 [英] How to replace multiple white spaces with one white space
本文介绍了如何与一个空格替换多个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我有一个字符串,如:
你好你怎么样?
我想,轮流多个空格变成一个空格的功能。
所以,我会得到:
你好你怎么样?
我知道我可以使用正则表达式或调用
字符串s =你好你怎么样?代替(,);
不过,我会多次调用它,以确保所有连续空格被只有一个替代。
时已经有一个内置该方法?
解决方案
字符串cleanedString = System.Text.RegularEx pressions.Regex.Replace(dirtyString,@\\ S + ,);
Let's say I have a string such as:
"Hello how are you doing?"
I would like a function that turns multiple spaces into one space.
So I would get:
"Hello how are you doing?"
I know I could use regex or call
string s = "Hello how are you doing?".replace(" "," ");
But I would have to call it multiple times to make sure all sequential whitespaces are replaced with only one.
Is there already a built in method for this?
解决方案
string cleanedString = System.Text.RegularExpressions.Regex.Replace(dirtyString,@"\s+"," ");
这篇关于如何与一个空格替换多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文