C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
OptimizationC# program that uses StringComparison
using System;
class Program
{
static void Main()
{
//
// Use the StringComparison enumerated type on the string input.
//
const string input = "The Dev Codes";
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
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.
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 EqualsC# program that compares strings insensitively
using System;
class Program
{
static void Main()
{
//
// Use three example strings.
//
string value1 = "The Dev Codes";
string value2 = "dot net Codex";
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
Note: The native code in the CLR has more optimizations than you can use in your C# code.
Tip: If in doubt, it is important to run a quick benchmark or you may degrade performance.
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