繁体   English   中英

删除字符串的间距

[英]Removing spacing from string

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

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

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)状态

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

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

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

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

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"

好吧,如果您有一个字符串x ,则可以

x.Trim();

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

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

如果您只想删除所有空白,则可以执行

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM