The C # string equality operator returns false, but I'm sure this should be true ... What?

I am trying to write a unit test for a piece of code that generates a large amount of text. I ran into a problem where the "expected" and "actual" lines seem equal, but Assert.AreEqual throws out, and both equality and Equals() operators return false. The result of GetHashCode() is different for both values.

However, putting both lines in text files and comparing with DiffMerge, I am told that they are the same.

Also, using Encoding.ASCII.GetBytes() for both values, and then using SequenceEquals to compare the resulting byte arrays, returns true.

The values ​​are 34K, so I will leave them here. Any ideas? I am completely at a dead end.

+5
string equality c #
source share
2 answers

What are the types of encoding files you upload to DiffMerge? If you have characters that do not match the encoding type, then it is likely that they will not appear in DiffMerge.

The string that is generated and the expected result probably have different character encodings. When you do ASCII.GetBytes , you convert everything to ASCII. So your strings are converted to ASCII and are equal in terms of the ASCII character set. However, they can still be unequal in other character sets (and still look at you the same way).

Also try making string.Compare(str1, str2, StringComparison.XXXX) and let us know what happens.

+5
source share

Scroll char through char and find what it thinks is different? The fact that writing to disk and comparing ASCII / text tells me that it is probably related to the carriage-return / line-line (which is somehow normalized during saving) or refers to some non-ASCII character (possibly , unicode whitespace) that will be deleted when saved as ASCII.

+10
source share

All Articles