string.Compare 是否可以 return 0 用于真正不相等的字符串?

Can string.Compare ever return 0 for genuinely unequal strings?

我在思考 string.Compare() 在 C# 中如何工作的数学。

在此方法调用中,两个不相等的字符串是否可能永远 return 0?

我指的是真正不相等的字符串,例如 "Herp" 和 "Derp",而不是 "Herp" 和 "Hěrp

不幸的是,除了基本的 null 情况外,source code for string.Compare 都是内部内容 - 在 .NET 之外。

我相信 this 是用于此的实际 C++ 代码,但很难确定。

我正在考虑的案例:

问这个问题没有特定原因 - 只是出于好奇。而且我之前没有看到它要求 C#!

我相信你的问题的答案在技术上是是的取决于你调用的重载,以及你传递的选项参数。 根据 MSDN docs 可以与对字符序数值有奇怪规则甚至跳过某些字符的文化进行比较:

Notes to Callers

Character sets include ignorable characters. The Compare(String, String) method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a culture-sensitive comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.

如果你想忽略 Culture 而只是比较 2 个字符串的原始值,你可以调用重载 String.Compare(s1, s2, StringComparison.OrdinalIgnoreCase)。这应该导致本质上是逐字节比较。 Docs:

Notes to Callers ... To recognize ignorable characters in your comparison, supply a value of StringComparison.Ordinal or OrdinalIgnoreCase for the comparisonType parameter.

请注意,"greater" 或 "lesser" 字符串的定义不一定很明显。例如,字符串 "abc" 是大于还是小于 "abcc"? .NET 很清楚,就字符串比较而言,它比较小。但是在依赖这种边缘情况之前仔细阅读文档是很好的:

The comparison terminates when an inequality is discovered or both strings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with remaining characters is considered greater. The return value is the result of the last comparison performed.