C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It contains several constants. Many methods use a parameter of type StringComparison. It specifies the internal behavior of methods such as IndexOf.
Tip: StringComparison values are used to tell string methods how to compare string data.
Example. First, we show how StringComparison affects the result of a common method used. The IndexOf method on the String type accepts an argument of type StringComparison. This dictates the specifics of the character searching.
In this example, all of the StringComparison constants result in the same value from the IndexOf method. The StringComparison enum is important to specify when you might deploy the application in other cultures.
Also: It helps when optimizing string performance. For the best performance, the StringComparison.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. // ... The *IgnoreCase constants can be used with a lowercase parameter. // ... Uppercase or mixed-case is fine too. // 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
Ordinal means number-based. It indicates that the characters in the string should be treated by their numeric value. The capital letter "A" would always represent the number 65 because of the ASCII layout.
The CurrentCulture enumerated constant indicates that you want the called function to use a search based on the current machine's culture. The effect varies based on the machine's setup.
Invariant: In this case, this term means that the culture itself is invariant and will remain the same without varying.
Note: MSDN describes invariant cultures as a type of globalization context that never changes but does not represent a specific culture.
Example 2. We use the string.Equals method with the StringComparison.OrdinalIgnoreCase argument. The first two arguments are the two strings you are trying to compare. The final argument is an instance of the StringComparison enumeration.
C# program that compares strings insensitively using System; class Program { static void Main() { // // Use three example strings. // string value1 = "Dot Net Perls"; string value2 = "The Developer Blog"; 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
This program prints the two string literals in the Console.WriteLine method calls. The string.Equals method called with OrdinalIgnoreCase returns true if the strings are equal when lowercase and uppercase are different.
Internally, this overload of string.Equals, which is actually an alias for String.Equals (uppercase S) from the System namespace, performs several argument validations. It also uses a switch to test the parameters.
But: It still performs better than simpler methods because it calls into native code that does the actual character comparisons.
Because: The native code in the CLR has more optimizations than you can use in your C# code.
Internals. String methods can use the StringComparison argument you pass them. In IndexOf, the StringComparison argument is used in a switch statement to determine the exact low-level method to invoke to do the character searching.
Also, methods on the CultureInfo and TextInfo types are used to do the actual search of characters. An ArgumentException is thrown if you somehow call the IndexOf method with an invalid StringComparison.
TextInfoArgumentException: Invalid Arguments
Performance. In my tests, the StringComparison.Ordinal overload is always the fastest on methods such as IndexOf, StartsWith and EndsWith. The difference is sometimes large. But will not greatly affect most programs.
Tip: If in doubt, it is important to run a quick benchmark or you may inadvertently degrade performance.
Summary. We saw the StringComparison enum. We used it an argument to important methods. The StringComparison argument provides a way to specify the internal behavior of the string methods such as IndexOf when searching for characters.
Often: There is no difference between the parameters. In micro-benchmarks the Ordinal constant is typically the fastest.