Home
Map
String Equals ExamplesUse the string.Equals method. Equals is benchmarked against the equality operator.
C#
This page was last reviewed on Aug 24, 2023.
String.Equals. This method compares strings. It differs from the Compare and CompareTo methods: Equals() tests strings for equality. A StringComparison can be used to ignore case.
String Compare, CompareTo
The Equals method is invoked with the method name Equals or with the equality operator. Most C# programs use the "==" operator syntax.
Equals example. Here we compare strings and test equality. The various parts of this code example code not all do the same thing, but each part does compare strings in some way.
Part 1 We use instance Equals on the first string—so it must not be null. If the string is null, we will get an exception.
null
Part 2 We use instance Equals by calling equals on the second string instead of the first string.
Part 3 The most common method for string comparisons is the equality operator.
operator
using System; string a = "a" + 1; string b = "a" + 1; // Part 1: use instance Equals() method. if (a.Equals(b)) { Console.WriteLine("a.Equals(b) = true"); } // Part 2: use instance method on other string. if (b.Equals(a)) { Console.WriteLine("b.Equals(a) = true"); } // Part 3: use op_Equality. if (a == b) { Console.WriteLine("a == b = true"); }
a.Equals(b) = true b.Equals(a) = true a == b = true
Compare. Next, we review the Compare methods. You can see the result of the string.Compare method being tested for a value of zero.
Tip When string.Compare returns zero, the strings are equal. It returns 1 or -1 depending on the sorted order of the string parameters.
Info This means that if string.Compare ever returns 1 or -1, the strings are not equal.
Sort
using System; string a = "a" + 1; string b = "a" + 1; // Use Compare methods. // ... This returns zero if the strings are equal. if (string.Compare(a, b) == 0) { Console.WriteLine("string.Compare(a, b) = 0"); } // ... CompareOrdinal returns 0 if numeric values equal. if (string.CompareOrdinal(a, b) == 0) { Console.WriteLine("string.CompareOrdinal(a, b) = 0"); } // ... Use instance Compare method. if (a.CompareTo(b) == 0) { Console.WriteLine("a.CompareTo(b) = 0"); }
string.Compare(a, b) = 0 string.CompareOrdinal(a, b) = 0 a.CompareTo(b) = 0
Benchmark. To test the code for Equals methods, I ran a benchmark of 2 strings that are equal but don't share a string pool reference. This avoids the intern pool.
string.Intern
Result The performance of Equals and the equality operator are about the same. The clearest code is the best option here.
Empty strings. You will find that, as FxCop recommends, testing against Length is usually the best way. The string.IsNullOrEmpty method deserves a special mention—it is fast and safe.
string.IsNullOrEmpty
Cases. You can easily compare strings in such a way that their case differences are ignored. We use values of the StringComparison enum.
StringComparison
A summary. We used the string.Equals and Compare methods. The string.Equals and equal operators have the same use normally. Compare and CompareTo are useful for sorting.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 24, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.