Masriyah
Masriyah

Reputation: 2505

Removing spacing from string

I am reading in two files and comparing them and one of the things i realized i needed to consider is removing the spacing because it is causing a difference which i don't want spacing to be an aspect of the difference so i wanted to remove it.

This is what i have so far:

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

Example Input file: Name (spaaaaaaace) Title (spaaaaaaace) Status

I would like it to be : Name Title Status

Upvotes: 0

Views: 157

Answers (4)

user2711965
user2711965

Reputation: 1825

Following would remove all white spaces (space, linebreak etc) from your string.

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

EDIT: For removing multiple spaces and replacing them by a single space you can try:

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

Result would be:

NoWhiteSpaceString = "Name Title Status"

Upvotes: 2

No Idea For Name
No Idea For Name

Reputation: 11577

well, if you have a string x, you can do

x.Trim();

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

that way the biggest space between words or sentences will be one whitespace

if you want to just remove every whitespace you can do

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

and that'll remove all whitespaces in your strings

Upvotes: 1

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9201

This will replace all multiple whitespaces with only one.

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

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

Just replace multiple white-spaces with a single white-space:

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

Upvotes: 5

Related Questions