Freigeben über


Performing Culture-Insensitive String Comparisons 

By default, the Compare method performs culture-sensitive and case-sensitive comparisons. However, an overload of the String.Compare method is provided that allows you to specify the culture to use by supplying a culture parameter. Application code should clearly demonstrate whether a string comparison is intended to be culture-sensitive or culture-insensitive. For culture-sensitive operations, specify the CultureInfo.CurrentCulture property as the culture parameter. For culture-insensitive operations, specify the CultureInfo.InvariantCulture property as the culture parameter.

If a security decision is based on the result of a string comparison, the operation should be culture-insensitive to ensure that the result is not affected by the value of CultureInfo.CurrentCulture. See Custom Case Mappings and Sorting Rules for an example that demonstrates how culture-sensitive string comparisons can produce inconsistent results.

Overloads of the String.CompareTo method perform culture-sensitive and case-sensitive comparisons by default. However, no overloads of this method are provided that allow you to specify a culture-insensitive comparison. For code clarity, it is recommended that you use the String.Compare method instead, specifying CultureInfo.CurrentCulture for culture-sensitive operations or CultureInfo.InvariantCulture for culture-insensitive operations.

Using the String.Compare Method

The following line of code demonstrates how to use the String.Compare method to perform a case-sensitive and culture-sensitive comparison of string1 and string2. CultureInfo.CurrentCulture is passed as the culture parameter, indicating that the case mappings and sorting rules for the CurrentCulture will be used.

Dim compareResult As Integer = String.Compare(string1, string2, False, CultureInfo.CurrentCulture)
int compareResult = String.Compare(string1, string2, false, CultureInfo.CurrentCulture);

For a complete code example that demonstrates how to use the String.Compare method to perform culture-sensitive comparisons, see Comparing and Sorting Data for a Specific Culture.

To make this operation culture-insensitive, you must specify CultureInfo.InvariantCulture as the culture parameter. This is demonstrated in the following example line of code.

Dim compareResult As Integer = String.Compare(string1, string2, False, CultureInfo.InvariantCulture)
int compareResult = String.Compare(string1, string2, false, CultureInfo.InvariantCulture);

The following code example demonstrates how to perform a culture-insensitive string comparison using the preceding line of code.

Imports System
Imports System.Globalization

Public Class CompareSample
    Public Shared Sub Main()
        Dim string1 As String = "file"
        Dim string2 As String = "FILE"
        
        Dim compareResult As Integer = String.Compare(string1, string2, _
            False, CultureInfo.InvariantCulture)   
        Console.WriteLine("A case-insensitive comparison of {0} and {1} is _
            {2}", string1, string2, compareResult)
    End Sub
End Class
using System;
using System.Globalization;

public class CompareSample
{
    public static void Main()
    {
        String string1 = "file";
        String string2 = "FILE";

        int compareResult = String.Compare(string1, string2, false, 
        CultureInfo.InvariantCulture);
        Console.WriteLine("A case-insensitive comparison of {0} and {1} is 
            {2}", string1, string2, compareResult);
    }
}

Using the String.CompareTo Method

The following example uses the String.CompareTo method to perform a comparison of string1 and string2. By default, the comparison is culture-sensitive and case-sensitive.

Dim compareResult As Integer = string1.CompareTo(string2)
int compareResult = string1.CompareTo(string2);

Because a culture parameter is not passed explicitly, the intent of the above example is ambiguous; you should replace the String.CompareTo method with the String.Compare method to make it clear whether you want this operation to be culture-sensitive or culture-insensitive. If your intention is to perform a case-sensitive and culture-sensitive comparison of string1 and string2, pass CultureInfo.CurrentCulture as the culture parameter. This causes the case mappings and sorting rules for the CurrentCulture to be used. This is demonstrated in the following example.

Dim compareResult As Integer = String.Compare(string1, string2, False, CultureInfo.CurrentCulture)
int compareResult = String.Compare(string1, string2, false, CultureInfo.CurrentCulture);

If your intention is to perform a case-sensitive and culture-insensitive comparison of string1 and string2, pass CultureInfo.InvariantCulture as the culture parameter. This causes the case mappings and sorting rules for the InvariantCulture to be used. This is demonstrated in the following example.

Dim compareResult As Integer = String.Compare(string1, string2, False, CultureInfo.InvariantCulture)
int compareResult = String.Compare(string1, string2, false, CultureInfo.InvariantCulture);

For a complete code example that demonstrates how to use the String.Compare method to obtain culture-insensitive results, see Using the String.Compare Method.

See Also

Reference

String.Compare Method
String.CompareTo Method

Concepts

Comparing and Sorting Data for a Specific Culture

Other Resources

Performing Culture-Insensitive String Operations