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

I was thinking math about how it string.Compare()

works in C #.

Is it possible for two unequal strings to ever return 0 when this method is called?

I mean strings that are really unequal like "Herp" and "Derp" rather than "Herp" and "Hěrp

Unfortunately, apart from the basic null cases, the source code forstring.Compare

- it's all internal stuff - outside of .NET.

I believe this is the actual C ++ code used to do this, but it's hard to do.

The cases I am considering:

  • strange ordinal behavior (just permutations of strings that end up being equal)
  • Integer overflow causing positive and negative numbers to be compared, resulting in 0
  • Anything else crazy, someone more implementation savvy mscorlib

    than me.

There is no particular reason to ask this - just curiosity. And I haven't seen this asked before for C #!

+3


source to share


1 answer


I believe the answer to your question is technically yes , depending on which overload you are calling and which parameter parameters you are passing. According to the MSDN docs, a comparison can be made against a culture that has strange rules for ordinal values ​​of characters, or even skips certain characters:

Notes to Callers

Character sets include non-knowledgeable characters. Comparison (String, String) ignores such characters when it does a culture-sensitive comparison. For example, if the following code runs on the .NET Framework 4 or later, a culture-sensitive comparison of "animal" to "ani-mal" (using a soft hyphen or U + 00AD) indicates that the two strings are equivalent.

If you want to ignore Culture and just compare the original values ​​from two strings, you can call the String.Compare (s1, s2, StringComparison.OrdinalIgnoreCase) overload. This should entail a step-by-step comparison. Documents :



Caller Notes ... recognize unfamiliar characters in your comparison, supply StringComparison.Ordinal or OrdinalIgnoreCase to compareType Parameter.

Note that the definition of "larger" or "smaller" strings is not necessarily obvious. For example, the string "abc" is greater or less than "abcc"? .NET is pretty clear that it is smaller for string comparison purposes. But it's good to read the docs carefully before relying on cases like this:

The comparison ends when an inequality is found or both strings were matched. However, if two strings compare equal to the end of one string, and the other string has characters, the rest of the string with the remaining characters is considered larger. The return value is the result of the last comparison made.

+4


source







All Articles