leora leora - 1 month ago 8
C# Question

How to not include line breaks when comparing two strings

i am comparing updates to two strings. i did a:

string1 != string2


and they turn out different. I put them in the "Add Watch" and i see the only difference is one has line breaks and the other doesnt'.:

string1 = "This is a test. \nThis is a test";
string2 = "This is a test. This is a test";


i basically want to do a compare but dont include line breaks. So if line break is the only difference then consider them equal.

Answer

A quick and dirty way, when performance isn't much of an issue:

string1.Replace("\n", "") != string2.Replace("\n", "")
Comments