Differences in String Compare Methods in C#

Differences in string compare methods in C#

Here are the rules for how these functions work:

stringValue.CompareTo(otherStringValue)

  1. null comes before a string
  2. it uses CultureInfo.CurrentCulture.CompareInfo.Compare, which means it will use a culture-dependent comparison. This might mean that ß will compare equal to SS in Germany, or similar

stringValue.Equals(otherStringValue)

  1. null is not considered equal to anything
  2. unless you specify a StringComparison option, it will use what looks like a direct ordinal equality check, i.e. ß is not the same as SS, in any language or culture

stringValue == otherStringValue

  1. Is not the same as stringValue.Equals().
  2. The == operator calls the static Equals(string a, string b) method (which in turn goes to an internal EqualsHelper to do the comparison.
  3. Calling .Equals() on a null string gets null reference exception, while on == does not.

Object.ReferenceEquals(stringValue, otherStringValue)

Just checks that references are the same, i.e. it isn't just two strings with the same contents, you're comparing a string object with itself.


Note that with the options above that use method calls, there are overloads with more options to specify how to compare.

My advice if you just want to check for equality is to make up your mind whether you want to use a culture-dependent comparison or not, and then use .CompareTo or .Equals, depending on the choice.

C#: What is the difference between CompareTo(String) and Equals(String)?

From MSDN:

string.CompareTo:

Compares this instance with a specified object or String and returns
an integer that indicates whether this instance precedes, follows, or
appears in the same position in the sort order as the specified object
or String.

string.Equals:

Determines whether two String objects have the same value.

In short, CompareTo is used for sorting. Equals is used to determine equality.

String.Equals vs String.Compare vs == in Action. Explanation needed

Why does the first comparison (string.compare) work and the other two
comparison methods does not work in THIS PARTICULAR CASE

There are invisible characters (particularly, a Left-to-Right mark (Thanks @MatthewWatson)) in your code. You can view them with any hex editor:

Sample Image

This is over-looked by string.Compare, while it isn't with string.Equals. You can see it in the 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.

What is difference between different string compare methods

Ripped from msdn

string.Equals

Determines whether this instance and a specified object, which must also be a String object, have the same value.

string.Compare
Compares two specified String objects and returns an integer that indicates their relative position in the sort order.

string.CompareTo
Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.

string.CompareOrdinal
Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

String equality operators
The predefined string equality operators are:

bool operator ==(string x, string y);
bool operator !=(string x, string y);
Two string values are considered equal when one of the following is true:

Both values are null.
Both values are non-null references to string instances that have identical lengths and identical characters in each character position.
The string equality operators compare string values rather than string references. When two separate string instances contain the exact same sequence of characters, the values of the strings are equal, but the references are different. As described in Section 7.9.6, the reference type equality operators can be used to compare string references instead of string values.

String.Compare() 2 Strings

It compares each character in the appering order: H = H, E = E, L = L, O > L and then it stops. So Helo > Hello simply because l is before o in the alphabet.

More info can be found on MSDN

Difference between the different overloads of String.Compare

Your answer is in the remarks for the second overload.

http://msdn.microsoft.com/en-us/library/cc190529.aspx

"The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it."

The other overload just uses the default culture.

Understanding String Comparison behaviour

Lower case comes before upper case .
That's true both for en-GB and for InvariantCulture.

If you want to the ASCII like behavior you should specify the additional CompareOptions.Ordinal parameter

See the following:

  • Difference between InvariantCulture and Ordinal string comparison

  • C# sorting strings small and capital letters

Sample code on repl.it:

using System;
using System.Globalization;
using System.Collections.Generic;

class MainClass
{
public static void Main(string[] args)
{

//All the following case sensitive comparisons puts d before D
Console.WriteLine("D".CompareTo("d"));
Console.WriteLine(String.Compare("D", "d", false));
Console.WriteLine(String.Compare("D", "d", false, CultureInfo.InvariantCulture));

//All the following case sensitive comparisons puts capital D before small letter d
Console.WriteLine(String.Compare("D", "d", CultureInfo.InvariantCulture, CompareOptions.Ordinal));

//The following is case insensitive
Console.WriteLine(String.Compare("D", "d", true));

//The default string ordering in my case is d before D
var list = new List<string>(new[] { "D", "d" });
list.Sort();
foreach (var s in list)
{
Console.WriteLine(s);
}
}
}

//Results on repl.it
//Mono C# compiler version 4.0.4.0
//
//1
//1
//1
//-32
//0
//d
//D

Good luck

Eyal

C# String Comparison equates to false

Unicode strings can be "binary" different, even if they are "semantically" the same.

Try normalizing your strings. For more information, see http://msdn.microsoft.com/en-us/library/System.String.Normalize.aspx



Related Topics



Leave a reply



Submit