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
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.

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
Internals, string.Equals. String.Equals performs argument validations. It uses a switch to test the parameters. It calls into native code that does the actual character comparisons.Switch

Note: The native code in the CLR has more optimizations than you can use in your C# code.

Internals, continued. The CultureInfo and TextInfo types are used to search characters. An ArgumentException is thrown if you call IndexOf() with an invalid StringComparison.TextInfoArgumentException
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.

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.

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

Tip: You can pass the references to constructors such as the Dictionary constructor.

Next: 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() { // Use these two StringComparer instances for demonstration. StringComparer comparer1 = StringComparer.Ordinal; StringComparer comparer2 = StringComparer.OrdinalIgnoreCase; // First test the results of 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")); // Test the results of 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
Usage. StringComparer helps with collections such as Dictionary, and the Array.Sort method overloads. You can pass the reference to these methods and constructors.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