8

I need to compare two strings ignoring whitespaces and newline characters, so the following strings should be equal:

"Initial directory structure.\r\n    \r\n    The directory tree has been changed"
"Initial directory structure.\n\nThe directory tree has been changed"

How can I implement it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • Check out the most upvoted answer to [this](http://stackoverflow.com/questions/4718965/c-sharp-string-comparison-ignoring-spaces-carriage-return-or-line-breaks) question – Nick Jul 27 '12 at 13:56
  • http://stackoverflow.com/questions/6859255/how-do-i-make-my-string-compare-not-sensitive-to-ignore-miner-differences-in-w/6859344#6859344 – Artem Vyshniakov Jul 27 '12 at 13:57
  • Couldn't you just remove new line and whitespace characters out of both and compare? – davidcollom Jul 27 '12 at 13:56
  • I gave an answer to a similar question here: http://stackoverflow.com/a/41408399/24874 – Drew Noakes Dec 31 '16 at 12:50

5 Answers5

15

how about:

string stringOne = "ThE    OlYmpics 2012!";
string stringTwo = "THe\r\n        OlympiCs 2012!";

string fixedStringOne = Regex.Replace(stringOne, @"\s+", String.Empty);
string fixedStringTwo = Regex.Replace(stringTwo, @"\s+", String.Empty);

bool isEqual = String.Equals(fixedStringOne, fixedStringTwo,
                              StringComparison.OrdinalIgnoreCase);

Console.WriteLine(isEqual);
Console.Read();
jim tollan
  • 22,305
  • 4
  • 49
  • 63
10

An alternative approach is to use the CompareOptions of String.Compare.

CompareOptions.IgnoreSymbols

Indicates that the string comparison must ignore symbols, such as white-space characters, punctuation, currency symbols, the percent sign, mathematical symbols, the ampersand, and so on.

String.Compare("foo\r\n   ", "foo", CompareOptions.IgnoreSymbols);

https://learn.microsoft.com/en-us/dotnet/api/system.globalization.compareoptions

Dan Bailiff
  • 1,513
  • 5
  • 23
  • 38
  • 4
    `CultureInfo.CurrentCulture.CompareInfo.Compare(lhs, rhs, CompareOptions.IgnoreSymbols) ` – bvj Aug 14 '19 at 04:39
2

copy the string then

xyz.Replace(" ", string.Empty);
xyz.Replace("\n", string.Empty);
Joshua
  • 542
  • 1
  • 4
  • 17
1

How about:

string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");

Then you can compare both.

Maybe throw that in to a helper function:

public bool SpacelessCompare(string a, string b){
    string compareA = a.Replace(Environment.NewLine, "").Replace(" ", "");
    string compareB = b.Replace(Environment.NewLine, "").Replace(" ", "");

    return compareA == compareB;
}
JohnD
  • 3,884
  • 1
  • 28
  • 40
musefan
  • 47,875
  • 21
  • 135
  • 185
  • Keep in mind when you get rid of spaces these two strings are marked as being the same: `"The red over" "There dover"` – JohnD Jul 27 '12 at 13:58
0
myString = myString.Replace("", "Initial directory structure.\r\n    \r\n    The directory tree has been changed");

Now just check if they are equal.

String.Compare(myString, "Initial directory structure.The directory tree has been changed")
x06265616e
  • 854
  • 11
  • 14