Home
C#
StringComparison and StringComparer
This page was last reviewed on May 17, 2023.
Dot Net Perls
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.
class
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.
Detail In this example, all of the StringComparison constants result in the same value from the IndexOf method.
String IndexOf
Also 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.
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
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 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.
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
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.
Detail 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.
Dictionary
Array.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 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 May 17, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.