折叠空间系列

collapsing series of spaces

重构现有代码。

他们正试图将传入的字符串中的任何一系列 space 折叠为单个 space。

肯定有更好的方法。

        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);