Problem
In C#, comparing strings is a breeze. In truth, there are a number of options. In the block below, I’ve listed a few. What I’m interested in learning about is the differences between them and when one should be used over the other. Is it necessary to avoid one at all costs? Are there any that I haven’t mentioned?
string testString = "Test";
string anotherString = "Another";
if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
(Note that in this example, I’m looking for equality, not less than or larger than, but feel free to remark on that as well.)
Asked by Craig
Solution #1
The following are the rules that govern how these functions work:
stringValue.CompareTo(otherStringValue)
stringValue.Equals(otherStringValue)
stringValue == otherStringValue
Object.ReferenceEquals(stringValue, otherStringValue)
Simply checks that references are the same, i.e. you’re comparing a string object with itself, not two strings with the same contents.
There are overloads with extra choices to indicate how to compare with the ones above that employ method calls.
If you merely want to check for equality, my advise is to first decide whether or not you want to apply a culture-dependent comparison, and then use. Alternatively,. Depending on the option, equals.
Answered by Lasse V. Karlsen
Solution #2
From MSDN:
when you’re only looking for equality I’m not sure if there’s a distinction between and. For the string class, equals and == are used. I’ll utilize it now and then. Equals or Object are the two options. In case someone comes along later and redefines the == operator for that class, I’ll use ReferenceEquals instead of == for my own classes.
Answered by Ed S.
Solution #3
Reflector is your friend if you’re ever wondering about the differences between BCL methods:-)
These are the rules I follow:
Identical match: EDIT: I used to always use the == operator because the object == operator is used to compare the object references inside Equals(string, string), but strA appears to be different. Overall, Equals(strB) is 1-11 percent faster than string. strA == strB, equals(strA, strB), and string OrdinalCompare (strA, strB). I loop tested using a StopWatch on both interned and non-interned string values, with the same and different string lengths, and varied sizes (1B to 5MB).
strA.Equals(strB)
Human-readable match (case-insensitive, Western cultures):
string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) == 0
Human-readable match (all other cultures, specified by CultureInfo as insensitive case/accent/kana/etc):
string.Compare(strA, strB, myCultureInfo) == 0
All other cultures: Human-readable match with custom rules:
CompareOptions compareOptions = CompareOptions.IgnoreCase
| CompareOptions.IgnoreWidth
| CompareOptions.IgnoreNonSpace;
string.Compare(strA, strB, CultureInfo.CurrentCulture, compareOptions) == 0
Answered by max
Solution #4
CompareTo is used for sorting, as Ed mentioned.
There is, however, a distinction between.Equals and ==.
== roughly equals 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 basic reason is that the following exception will be thrown:
string a = null;
string b = "foo";
bool equal = a.Equals(b);
The following, on the other hand, will not:
string a = null;
string b = "foo";
bool equal = a == b;
Answered by Jonathan C Dickinson
Solution #5
The articles New Recommendations for Using Strings in Microsoft.NET 2.0 and Best Recommendations for Using Strings in the.NET Framework provide good explanations and practices for string comparison concerns.
Each of the methods described (and others) serves a specific function. The main difference between them is the default StringComparison Enumeration that they use. There are various possibilities:
Each of the comparison types above is aimed towards a distinct use case:
StringComparison Enumeration and overloads for string comparison methods have been available since.NET 2.0.
IComparable implementation that is type safe. The CompareTo method is used to compare two objects. CurrentCulture is the default interpretation.
Usage:
Thus
A String Class static member with multiple overloads. CurrentCulture is the default interpretation.
Overloaded and overridden from the Object class for type safety. Ordinal is the default interpretation. Take note of the following:
There is also another way to deal with string comparisons especially aims to sorting:
Answered by Ryszard Dżegan
Post is based on https://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp