Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings ignoring new line characters and white spaces

Tags:

string

c#

.net

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?

like image 574
Daniel Peñalba Avatar asked Jul 27 '12 13:07

Daniel Peñalba


1 Answers

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();
like image 132
jim tollan Avatar answered Sep 24 '22 00:09

jim tollan