从字符串中删除间距

时间:2013-08-29 13:40:38

标签: c# winforms linq

我正在阅读两个文件并对它们进行比较,我意识到需要考虑的事情之一就是删除间距,因为它造成了差异,我不希望间距是差异的一个方面,所以我想要删除它。

这是我到目前为止所做的:

Dictionary<string, int> Comparer = new Dictionary<string, int>();
            string line;
            using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
            {
                while (sr.Peek() >= 0 )
                {
                    line = sr.ReadLine();

                    if (Comparer.ContainsKey(line))
                        Comparer[line]++;
                    else
                        Comparer[line] = 1;
                }
            }

using (StreamReader sr = new StreamReader(openFileDialog2.FileName))
            {
                while (sr.Peek() >= 0)
                {
                    line = sr.ReadLine();
                    if (Comparer.ContainsKey(line))
                        Comparer[line]--;
                    else
                        Comparer[line] = -1;
                }
            }

            int mismatches = 0;

            var firstFileChanges = new List<string>();
            var secondFileChanges = new List<string>();

            System.Text.StringBuilder theStringBuilder = new System.Text.StringBuilder();
            foreach (KeyValuePair<string, int> kvp in Comparer)
            {
                if (kvp.Value != 0)
                {
                    mismatches++;
                    string InWhich = kvp.Value > 0 ? openFileDialog1.FileName : openFileDialog2.FileName;

                    if (InWhich == openFileDialog1.FileName)
                    {
                        firstFileChanges.Add(kvp.Key);
                    }
                    else
                    {
                        secondFileChanges.Add(kvp.Key);
                    }
               }
            }
            if (firstFileChanges.Count > 0)
            {
                theStringBuilder.Append("ADDED IN " + openFileDialog1.SafeFileName+": \n");

                int counter1 = 0;
                foreach (string row in firstFileChanges)
                {
                    if (counter1 > 0)
                    {
                        theStringBuilder.Append("\n ");
                    }
                    theStringBuilder.Append(row);
                    counter1 += 1;
                }
               theStringBuilder.AppendLine();
            }

            if (secondFileChanges.Count > 0)
            {
                theStringBuilder.Append("\nDELETED FROM "+openFileDialog2.SafeFileName+": \n");

                int counter2 = 0;
                foreach (string row in secondFileChanges)
                {
                    if (counter2 > 0)
                    {
                        theStringBuilder.Append("\n ");
                    }

                    theStringBuilder.Append(row);

                    counter2 += 1;
                }
            }

示例输入文件: 姓名(spaaaaaaace)标题(spaaaaaaace)状态

我希望它是: 姓名标题状态

4 个答案:

答案 0 :(得分:5)

只需用一个空格替换多个空格:

string cleanedLine = System.Text.RegularExpressions.Regex.Replace(line,@"\s+"," ");
if (Comparer.ContainsKey( cleanedLine ))
    Comparer[ cleanedLine ] ++;
else
    Comparer[ cleanedLine ] = 1;

答案 1 :(得分:2)

以下将删除字符串中的所有空格(空格,换行符等)。

string NoWhiteSpaceString = new String(yourString
                                     .Where(r=> !char.IsWhiteSpace(r))
                                     .ToArray());

编辑:要删除多个空格并将其替换为单个空格,您可以尝试:

string yourString = "Name           Title           Status";
string NoWhiteSpaceString =
    string.Join(" ", 
            yourString.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries));

结果将是:

NoWhiteSpaceString = "Name Title Status"

答案 2 :(得分:1)

好吧,如果你有一个字符串x,你可以做

x.Trim();

while(x.Contains("  "))
{
   x.Replace("  ", " ");
}
这样,单词或句子之间最大的空间就是一个空格

如果你想删除你可以做的每个空格

x.Replace(" ", "");
x.Replace("\t", "");

并且会删除字符串中的所有空格

答案 3 :(得分:1)

这将只用一个空格替换所有多个空格。

string input = "Name      Title        Status";
string result = string.Join(" ", input.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)); //result is "Name Title Status"