日期:2014-05-18  浏览次数:20425 次

怎样用正则表达式去除多余空格
比如String   aa= "不       好     意       思         谢               谢 ";
我想去除多余的空格,不管多少都替换成一个,变成 "不   好   意   谢   谢 "
================================解决了马上结账,本人不喜欢欠单

------解决方案--------------------
try

string yourStr = ..........;
string result = Regex.Replace(yourStr, @ "\s+ ", " ");
------解决方案--------------------
1。
直接使用 String.Replace 方法
aa = aa.Replace( " ", " ");

2。
Regex

string str1 = Regex.Replace( "不 好 意 思 谢 谢 ", @ "\s+ ", " ");
------解决方案--------------------
不好意思
看错了

string s = "a b c f ";
Regex r = new Regex(@ "\s{2,} ");
s = r.Replace(s, " ");
Console.WriteLine(s);
Console.WriteLine(s.Length);
------解决方案--------------------
Regex.Replace( "不 好 意 思 谢 谢 ", @ "\s{1,} ", " ", RegexOptions.IgnoreCase)