284

Comparing string in C# is pretty simple. In fact there are several ways to do it. I have listed some in the block below. What I am curious about are the differences between them and when one should be used over the others? Should one be avoided at all costs? Are there more I haven't listed?

string testString = "Test";
string anotherString = "Another";

if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}

(Note: I am looking for equality in this example, not less than or greater than but feel free to comment on that as well)

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
Craig
  • 11,614
  • 13
  • 44
  • 62

11 Answers11

241

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.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 6
    "stringValue.Equals(otherStringValue): null is not equal to null" Lol, I'd say not. null equals ObjectReferenceNotSet exception. – Kevin Feb 04 '09 at 22:25
  • 32
    == is not the same as .Equals()... The == operator calls the static Equals(string a, string b) method (which in turn goes to an internal EqualsHelper to do the comparison. Calling .Equals on a null string gets null reference exc., while on == does not. – Dan C. Feb 05 '09 at 10:24
  • 2
    On the other hand, .Equals is slightly faster (one less method call internally), but less readable - arguably, of course :). – Dan C. Feb 05 '09 at 10:24
  • I was thinking '==' will do reference comparisons and object.equals will do value comparisons.How '==' and string.equals work as the same? – amesh Aug 09 '12 at 13:45
  • the == difference should be called out in the answer, that is a pretty big difference. – Joe Cartano Oct 29 '13 at 23:54
  • could you add the **case** sensitivity to each one? – serge May 28 '21 at 12:00
76

From MSDN:

"The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method."

They suggest using .Equals instead of .CompareTo when looking solely for equality. I am not sure if there is a difference between .Equals and == for the string class. I will sometimes use .Equals or Object.ReferenceEquals instead of == for my own classes in case someone comes along at a later time and redefines the == operator for that class.

FMFF
  • 1,652
  • 4
  • 32
  • 62
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 22
    Did that ever happened to you? (Redefining == )... I see it as waaaay too defensive programming =) – juan Feb 04 '09 at 22:48
  • Yes, that's why I now use Object.ReferenceEquals when I am looking for object equality :). It may be a tad over-defensive, but I am not maniacal about it and truthfully this situation doesn't pop up very often. – Ed S. Feb 04 '09 at 23:17
  • I doubt that this 'defensive coding' is useful. What if the class owner needs to override the == operator, then finds out no one is using it? – Dave Van den Eynde Feb 27 '12 at 14:15
  • 1
    @DaveVandenEynde: Yeah... I wrote this a while back. I don't do this regularly, only overriding .Equals when appropriate. – Ed S. Feb 27 '12 at 17:46
  • 1
    Microsoft's recommendation is recorded here: [Best Practices for Using Strings in the .NET Framework](http://msdn.microsoft.com/en-us/library/dd465121.aspx) – JJS Feb 26 '13 at 19:48
  • This "answer" does not answer the question. – sisharp Jun 03 '13 at 13:56
  • The suggestion of defending against how '==' might behave really begs the question of what to do about '+', '-' or '.'. See here for a good case study: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/blob/uinverse/src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/strategies/comparators/integercomparator/IntegerForEqualityComparator.java – Elliott Beach Jan 26 '23 at 15:54
50

If you are ever curious about differences in BCL methods, Reflector is your friend :-)

I follow these guidelines:

Exact match: EDIT: I previously always used == operator on the principle that inside Equals(string, string) the object == operator is used to compare the object references but it seems strA.Equals(strB) is still 1-11% faster overall than string.Equals(strA, strB), strA == strB, and string.CompareOrdinal(strA, strB). I loop tested with a StopWatch on both interned/non-interned string values, with same/different string lengths, and varying sizes (1B to 5MB).

strA.Equals(strB)

Human-readable match (Western cultures, case-insensitive):

string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) == 0

Human-readable match (All other cultures, insensitive case/accent/kana/etc defined by CultureInfo):

string.Compare(strA, strB, myCultureInfo) == 0

Human-readable match with custom rules (All other cultures):

CompareOptions compareOptions = CompareOptions.IgnoreCase
                              | CompareOptions.IgnoreWidth
                              | CompareOptions.IgnoreNonSpace;
string.Compare(strA, strB, CultureInfo.CurrentCulture, compareOptions) == 0
Noctis
  • 11,507
  • 3
  • 43
  • 82
max
  • 501
  • 4
  • 3
20

As Ed said, CompareTo is used for sorting.

There is a difference, however, between .Equals and ==.

== resolves to essentially the following code:

if(object.ReferenceEquals(left, null) && 
   object.ReferenceEquals(right, null))
    return true;
if(object.ReferenceEquals(left, null))
    return right.Equals(left);
return left.Equals(right);

The simple reason is the following will throw an exception:

string a = null;
string b = "foo";

bool equal = a.Equals(b);

And the following will not:

string a = null;
string b = "foo";

bool equal = a == b;
Community
  • 1
  • 1
Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46
15

Good explanation and practices about string comparison issues may be found in the article New Recommendations for Using Strings in Microsoft .NET 2.0 and also in Best Practices for Using Strings in the .NET Framework.


Each of mentioned method (and other) has particular purpose. The key difference between them is what sort of StringComparison Enumeration they are using by default. There are several options:

  • CurrentCulture
  • CurrentCultureIgnoreCase
  • InvariantCulture
  • InvariantCultureIgnoreCase
  • Ordinal
  • OrdinalIgnoreCase

Each of above comparison type targets different use case:

  • Ordinal
    • Case-sensitive internal identifiers
    • Case-sensitive identifiers in standards like XML and HTTP
    • Case-sensitive security-related settings
  • OrdinalIgnoreCase
    • Case-insensitive internal identifiers
    • Case-insensitive identifiers in standards like XML and HTTP
    • File paths (on Microsoft Windows)
    • Registry keys/values
    • Environment variables
    • Resource identifiers (handle names, for example)
    • Case insensitive security related settings
  • InvariantCulture or InvariantCultureIgnoreCase
    • Some persisted linguistically-relevant data
    • Display of linguistic data requiring a fixed sort order
  • CurrentCulture or CurrentCultureIgnoreCase
    • Data displayed to the user
    • Most user input

Note, that StringComparison Enumeration as well as overloads for string comparison methods, exists since .NET 2.0.


String.CompareTo Method (String)

Is in fact type safe implementation of IComparable.CompareTo Method. Default interpretation: CurrentCulture.

Usage:

The CompareTo method was designed primarily for use in sorting or alphabetizing operations

Thus

Implementing the IComparable interface will necessarily use this method

String.Compare Method

A static member of String Class which has many overloads. Default interpretation: CurrentCulture.

Whenever possible, you should call an overload of the Compare method that includes a StringComparison parameter.

String.Equals Method

Overriden from Object class and overloaded for type safety. Default interpretation: Ordinal. Notice that:

The String class's equality methods include the static Equals, the static operator ==, and the instance method Equals.


StringComparer class

There is also another way to deal with string comparisons especially aims to sorting:

You can use the StringComparer class to create a type-specific comparison to sort the elements in a generic collection. Classes such as Hashtable, Dictionary, SortedList, and SortedList use the StringComparer class for sorting purposes.

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56
  • 2
    According to some other posts on SO, all the methods other than the ordinal ones have cases where Compare(a,b) and Compare(b,a) can both return 1, and the bug has been classed as "won't be fixed". As such, I'm not sure any such comparisons have *any* use case. – supercat Mar 03 '14 at 23:55
  • @supercat can you link to that, or give an example? – Noctis Nov 04 '14 at 06:11
  • 1
    See http://stackoverflow.com/questions/17599084/c-sharp-sortedliststring-tvalue-containskey-for-successfully-added-key-return for a discussion of the issue. – supercat Nov 04 '14 at 12:59
7

Not that performance usually matters with 99% of the times you need to do this, but if you had to do this in a loop several million times I would highly suggest that you use .Equals or == because as soon as it finds a character that doesn't match it throws the whole thing out as false, but if you use the CompareTo it will have to figure out which character is less than the other, leading to slightly worse performance time.

If your app will be running in different countries, I'd recommend that you take a look at the CultureInfo implications and possibly use .Equals. Since I only really write apps for the US (and don't care if it doesn't work properly by someone), I always just use ==.

viggity
  • 15,039
  • 7
  • 88
  • 96
5

In the forms you listed here, there's not much difference between the two. CompareTo ends up calling a CompareInfo method that does a comparison using the current culture; Equals is called by the == operator.

If you consider overloads, then things get different. Compare and == can only use the current culture to compare a string. Equals and String.Compare can take a StringComparison enumeration argument that let you specify culture-insensitive or case-insensitive comparisons. Only String.Compare allows you to specify a CultureInfo and perform comparisons using a culture other than the default culture.

Because of its versatility, I find I use String.Compare more than any other comparison method; it lets me specify exactly what I want.

OwenP
  • 24,950
  • 13
  • 65
  • 102
2

One BIG difference to note is .Equals() will throw an exception if first string is null, Whereas == will not.

       string s = null;
        string a = "a";
        //Throws {"Object reference not set to an instance of an object."}
        if (s.Equals(a))
            Console.WriteLine("s is equal to a");
        //no Exception
        if(s==a)
            Console.WriteLine("s is equal to a");
Rauld
  • 980
  • 2
  • 10
  • 19
1
  • s1.CompareTo(s2): Do NOT use if primary purpose is to determine whether two strings are equivalent
  • s1 == s2: Cannot ignore case
  • s1.Equals(s2, StringComparison): Throws NullReferenceException if s1 is null
  • String.Equals(s2, StringComparison): By process of eliminiation, this static method is the WINNER (assuming a typical use case to determine whether two strings are equivalent)!
John DiFini
  • 96
  • 2
  • 6
-1

Using .Equals is also a lot easier to read.

hometoast
  • 11,522
  • 5
  • 41
  • 58
-9

with .Equals, you also gain the StringComparison options. very handy for ignoring case and other things.

btw, this will evaluate to false

string a = "myString";
string b = "myString";

return a==b

Since == compares the values of a and b (which are pointers) this will only evaluate to true if the pointers point to the same object in memory. .Equals dereferences the pointers and compares the values stored at the pointers. a.Equals(b) would be true here.

and if you change b to:

b = "MYSTRING";

then a.Equals(b) is false, but

a.Equals(b, StringComparison.OrdinalIgnoreCase) 

would be true

a.CompareTo(b) calls the string's CompareTo function which compares the values at the pointers and returns <0 if the value stored at a is less than the value stored at b, returns 0 if a.Equals(b) is true, and >0 otherwise. However, this is case sensitive, I think there are possibly options for CompareTo to ignore case and such, but don't have time to look now. As others have already stated, this would be done for sorting. Comparing for equality in this manner would result in unecessary overhead.

I'm sure I'm leaving stuff out, but I think this should be enough info to start experimenting if you need more details.

Tom
  • 1,234
  • 1
  • 18
  • 39
David
  • 1
  • 9
    The a==b part is incorrect. The == operator is effectively overloaded for the String class and it compares the values regardless of the actual references. – Goyuix Apr 01 '10 at 23:20