简体   繁体   English

C#字符串相等运算符返回false,但我很确定它应该是真的......什么?

[英]C# string equality operator returns false, but I'm pretty sure it should be true… What?

I'm trying to write a unit test for a piece of code that generates a large amount of text. 我正在尝试为生成大量文本的代码编写单元测试。 I've run into an issue where the "expected" and "actual" strings appear to be equal, but Assert.AreEqual throws, and both the equality operator and Equals() return false. 我遇到了一个问题,其中“预期”和“实际”字符串似乎相等,但Assert.AreEqual抛出,并且相等运算符和Equals()返回false。 The result of GetHashCode() is different for both values as well. GetHashCode()的结果对于两个值也是不同的。

However, putting both strings into text files and comparing with DiffMerge tells me they're the same. 但是,将两个字符串放入文本文件并与DiffMerge进行比较告诉我它们是相同的。

Additionally, using Encoding.ASCII.GetBytes() on both values and then using SequenceEquals to compare the resulting byte arrays returns true. 此外,在两个值上使用Encoding.ASCII.GetBytes()然后使用SequenceEquals来比较结果字节数组返回true。

The values are 34KB each, so I'll hold off putting them here for now. 这些值每个都是34KB,所以我暂时把它们放在这里。 Any ideas? 有任何想法吗? I'm completely stumped. 我完全难过了。

Loop through char by char and find which it thinks is different? 通过char循环char并找到它认为不同的? The fact that writing it to disk and comparing the ASCII / text tells me that it is probably either carriage-return / line-feed related (which is somehow normalized during save), or relates to some non-ASCII character (maybe a high-unicode whitespace), which will be stripped when saving as ASCII. 将它写入磁盘并比较ASCII /文本的事实告诉我,它可能是回车/换行相关(在保存期间以某种方式标准化),或者与某些非ASCII字符(可能是高 - 相关) unicode whitespace),在保存为ASCII时将被剥离。

What are the encoding types of the files you are feeding into DiffMerge? 您正在为DiffMerge提供的文件的编码类型是什么? If you have characters that don't match the encoding type, then there is a chance they won't show up in DiffMerge. 如果您的字符与编码类型不匹配,则有可能它们不会显示在DiffMerge中。

The string that is being generated and the expected result probably have different character encodings. 正在生成的字符串和预期结果可能具有不同的字符编码。 When you are doing ASCII.GetBytes , you are converting everything into ASCII. 当您使用ASCII.GetBytes ,您将所有内容转换为ASCII。 So, your strings are being converted to ASCII and are equal in terms of the ASCII character set. 因此,您的字符串将转换为ASCII,并且在ASCII字符集方面相同。 However, they can still be unequal in other character sets (and still "look" the same to you). 但是,它们在其他字符集中仍然可能不相等(并且仍然“看起来”与您相同)。

Also, try doing a string.Compare(str1, str2, StringComparison.XXXX) and let us know what happens. 另外,尝试执行string.Compare(str1, str2, StringComparison.XXXX)并告诉我们发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM