C# StringComparison and StringComparer

Use the StringComparison enum type, and the StringComparer Ordinal enum.
StringComparison, StringComparer. These 2 types help us compare strings. StringComparison is an enum—it can be passed to methods like IndexOf.Enum
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.StringsClass
StringComparison example. This enum affects the result of a common method used. The IndexOf method on the String type accepts an argument of type StringComparison.

And: The StringComparison argument dictates the specifics of the character searching.

IndexOf: In this example, all of the StringComparison constants result in the same value from the IndexOf method.

Also: StringComparison helps when optimizing string performance. For the best performance, the Ordinal enum is useful.

C# program that uses StringComparison using System; class Program { static void Main() { // // 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)); } } Output 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.

And: The final argument is an instance of the StringComparison enum. Any valid value can be used. We can access it like any other enum.

Here: Equals() called with OrdinalIgnoreCase returns true if the strings are equal when lowercase and uppercase are different.

String Equals
C# program that compares strings insensitively 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"); } } } Output value1 equals value2 value1 equals value3
StringComparer example. StringComparer specifies how strings are compared. It is used as an argument to certain constructors and methods.

Info: StringComparer influences performance of collections such as Dictionary. Common values include Ordinal and OrdinalIgnoreCase.

Part 1: The program gets references to the StringComparer class with the properties StringComparer.Ordinal and OrdinalIgnoreCase.

Part 2: The Equals and Compare methods are called. With OrdinalIgnoreCase, lowercase and uppercase letters are treated as equal.

C# program that uses StringComparer instances 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")); } } Output True False -1 0 1 True False True -1 0 1
Notes, ordinal. Ordinal means number-based. It indicates that the characters in the string should be treated by their numeric value.

Thus: The capital letter "A" would always represent the number 65 because of the ASCII layout.

Also: The CurrentCulture enum indicates that you want the called function to use a search based on the current machine's culture.

Invariant: Microsoft describes an invariant culture as a globalization context that never changes but represents no specific culture.

Performance, StringComparison. In tests, the StringComparison.Ordinal overload is the fastest on methods such as IndexOf, StartsWith and EndsWith.

Tip: If in doubt, it is important to run a quick benchmark or you may degrade performance.

Usage, StringComparer. StringComparer helps with collections such as Dictionary, and the Array.Sort method overloads. We can pass a reference.DictionaryArray.Sort
A summary. StringComparison lets us specify the internal behavior of the string methods (such as IndexOf). StringComparer also provides a way to specify string comparisons.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to