重构现有代码。
他们试图折叠传递给单个空格的字符串中的任何空格。
当然有更好的方法。
for (int i = 0; i < 25; i++)
{
str = str.Replace(" "," ");
}
重构现有代码。
他们试图折叠传递给单个空格的字符串中的任何空格。
当然有更好的方法。
for (int i = 0; i < 25; i++)
{
str = str.Replace(" "," ");
}
System.Text.RegularExpressions.Regex.Replace(str,@"\s+"," ");
,
使用分割字符串
List<string> spiltList = yourStr.Split(' ').ToList();
从集合中删除空字符串。
spiltList.RemoveAll(e => string.IsNullOrWhiteSpace(e));
将字符串列表加入单个字符串
string result = string.Join(" ",spiltList);