StringComparison
, StringComparer
These 2 types help us compare strings. StringComparison
is an enum
—it can be passed to methods like IndexOf
.
Meanwhile, StringComparer
is a class
we can use to compare 2 strings in a certain way. We can use both of these types by including the System
namespace in a C# program.
StringComparison
exampleThis enum
affects the result of a common method used. The IndexOf
method on the String
type accepts an argument of type StringComparison
.
StringComparison
argument dictates the specifics of the character searching.StringComparison
constants result in the same value from the IndexOf
method.StringComparison
helps when optimizing string
performance. For the best performance, the Ordinal enum
is useful.using System; // // Use the StringComparison enumerated type on the string input. // const string input = "Dot Net Perls"; Console.WriteLine(input.IndexOf("Net", StringComparison.Ordinal)); Console.WriteLine(input.IndexOf("net", StringComparison.OrdinalIgnoreCase)); Console.WriteLine(input.IndexOf("Net", StringComparison.CurrentCulture)); Console.WriteLine(input.IndexOf("net", StringComparison.CurrentCultureIgnoreCase)); Console.WriteLine(input.IndexOf("Net", StringComparison.InvariantCulture)); Console.WriteLine(input.IndexOf("net", StringComparison.InvariantCultureIgnoreCase));4 4 4 4 4 4
OrdinalIgnoreCase
We use the string.Equals
method with the StringComparison
OrdinalIgnoreCase
argument. The first 2 arguments are the 2 strings you are trying to compare.
StringComparison
enum
. Any valid value can be used. We can access it like any other enum
.Equals()
called with OrdinalIgnoreCase
returns true if the strings are equal when lowercase and uppercase are different.using System; class Program { static void Main() { // // Use three example strings. // string value1 = "Dot Net Perls"; string value2 = "dot net perls"; string value3 = "DOT NET PERLS"; // // Compare strings case-insensitively. // if (string.Equals(value1, value2, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("value1 equals value2"); } // // Compare strings case-insensitively again. // if (string.Equals(value1, value3, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("value1 equals value3"); } } }value1 equals value2 value1 equals value3
StringComparer
exampleStringComparer
specifies how strings are compared. It is used as an argument to certain constructors and methods.
StringComparer
influences performance of collections such as Dictionary
. Common values include Ordinal and OrdinalIgnoreCase
.StringComparer
class
with the properties StringComparer.Ordinal
and OrdinalIgnoreCase
.Equals
and Compare methods are called. With OrdinalIgnoreCase
, lowercase and uppercase letters are treated as equal.using System; class Program { static void Main() { // Part 1: use these 2 StringComparer instances. StringComparer comparer1 = StringComparer.Ordinal; StringComparer comparer2 = StringComparer.OrdinalIgnoreCase; // Part 2: test the StringComparers. // ... First test the Ordinal comparer. Console.WriteLine(comparer1.Equals("value-1", "value-1")); // True Console.WriteLine(comparer1.Equals("value-1", "VALUE-1")); // False Console.WriteLine(comparer1.Compare("a", "b")); Console.WriteLine(comparer1.Compare("a", "a")); Console.WriteLine(comparer1.Compare("b", "a")); // ... Next test the OrdinalIgnoreCase comparer. Console.WriteLine(comparer2.Equals("value-1", "value-1")); // True Console.WriteLine(comparer2.Equals("value-a", "value-b")); // False Console.WriteLine(comparer2.Equals("value-1", "VALUE-1")); // True Console.WriteLine(comparer2.Compare("a", "B")); Console.WriteLine(comparer2.Compare("a", "A")); Console.WriteLine(comparer2.Compare("b", "A")); } }True False -1 0 1 True False True -1 0 1
Ordinal means number-based. It indicates that the characters in the string should be treated by their numeric value.
CurrentCulture
enum
indicates that you want the called function to use a search based on the current machine's culture.StringComparison
In tests, the StringComparison.Ordinal
overload is the fastest on methods such as IndexOf
, StartsWith
and EndsWith
.
StringComparer
StringComparer
helps with collections such as Dictionary
, and the Array.Sort
method overloads. We can pass a reference.
StringComparison
lets us specify the internal behavior of the string
methods (such as IndexOf
). StringComparer
also provides a way to specify string
comparisons.