0

我正在阅读两个文件并比较它们,我意识到我需要考虑的一件事是删除间距,因为它会导致差异,我不希望间距成为差异的一个方面,所以我想删除它.

这是我到目前为止所拥有的:

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

4 回答 4

5

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

string cleanedLine = System.Text.RegularExpressions.Regex.Replace(line,@"\s+"," ");
if (Comparer.ContainsKey( cleanedLine ))
    Comparer[ cleanedLine ] ++;
else
    Comparer[ cleanedLine ] = 1;
于 2013-08-29T13:51:12.773 回答
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"
于 2013-08-29T13:46:10.727 回答
1

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

string input = "Name      Title        Status";
string result = string.Join(" ", input.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)); //result is "Name Title Status"
于 2013-08-29T13:55:56.897 回答
1

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

x.Trim();

while(x.Contains("  "))
{
   x.Replace("  ", " ");
}

这样单词或句子之间的最大空格将是一个空格

如果您只想删除可以执行的每个空格

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

这将删除字符串中的所有空格

于 2013-08-29T13:45:31.203 回答