执行不区分区域性的字符串比较Performing Culture-Insensitive String Comparisons

默认情况下,String.Compare 方法执行区分区域性和区分大小写的比较。By default, the String.Compare method performs culture-sensitive and case-sensitive comparisons. 此方法还包括多个重载,这些重载提供了一个 culture 参数和一个 comparisonType 参数,前者允许你指定要使用的区域性,后者允许你指定要使用的比较规则。This method also includes several overloads that provide a culture parameter that lets you specify the culture to use, and a comparisonType parameter that lets you specify the comparison rules to use. 调用这些方法(而非调用默认重载)将消除与特定方法调用中使用的规则相关的任何歧义,并阐明某个特定比较是区分区域性的还是不区分区域性的。Calling these methods instead of the default overload removes any ambiguity about the rules used in a particular method call, and makes it clear whether a particular comparison is culture-sensitive or culture-insensitive.

备注

String.CompareTo 方法的两种重载都执行区分区域性且区分大小写的比较;你不能使用此方法来执行不区分区域性的比较。Both overloads of the String.CompareTo method perform culture-sensitive and case-sensitive comparisons; you cannot use this method to perform culture-insensitive comparisons. 为了使代码简单明了,建议你改用 String.Compare 方法。For code clarity, we recommend that you use the String.Compare method instead.

对于区分区域性的操作,请将 StringComparison.CurrentCultureStringComparison.CurrentCultureIgnoreCase 枚举值指定为 comparisonType 参数。For culture-sensitive operations, specify the StringComparison.CurrentCulture or StringComparison.CurrentCultureIgnoreCase enumeration value as the comparisonType parameter. 若要使用除当前区域性之外的指定区域性来执行区域性敏感型比较,请将表示相应区域性的 CultureInfo 对象指定为 culture 参数。If you want to perform a culture-sensitive comparison using a designated culture other than the current culture, specify the CultureInfo object that represents that culture as the culture parameter.

String.Compare 方法所支持的不区分区域性的字符串比较可以是语义的(基于固定区域性的排序约定)或非语义的(基于字符串中字符的序号值)。The culture-insensitive string comparisons supported by the String.Compare method are either linguistic (based on the sorting conventions of the invariant culture) or non-linguistic (based on the ordinal value of the characters in the string). 大多数不区分区域性的字符串比较是非语义的。Most culture-insensitive string comparisons are non-linguistic. 对于这些比较,请将 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase 枚举值指定为 comparisonType 参数。For these comparisons, specify the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase enumeration value as the comparisonType parameter. 例如,如果安全决策(例如,用户名或密码比较)基于字符串比较的结果,则操作应不区分区域性且是非语义的,以确保结果不受特定区域性或语言的约定的影响。For example, if a security decision (such as a user name or password comparison) is based on the result of a string comparison, the operation should be culture-insensitive and non-linguistic to ensure that the result is not affected by the conventions of a particular culture or language.

如果你希望以一致方式处理来自多个区域性的语义相关字符串,请使用不区分区域性的语义字符串比较。Use culture-insensitive linguistic string comparison if you want to handle linguistically relevant strings from multiple cultures in a consistent way. 例如,如果你的应用程序在列表框中显示使用多个字符集的字词,则不管当前区域性如何,你可能都需要按相同的顺序来显示这些字词。For example, if your application displays words that use multiple character sets in a list box, you might want to display words in the same order regardless of the current culture. 对于不区分区域性的语义比较,.NET Framework 将定义一个基于英语的语义约定的固定区域性。For culture-insensitive linguistic comparisons, the .NET Framework defines an invariant culture that is based on the linguistic conventions of English. 若要执行不区分区域性的语义比较,请将 StringComparison.InvariantCultureStringComparison.InvariantCultureIgnoreCase 指定为 comparisonType 参数。To perform a culture-insensitive linguistic comparison, specify StringComparison.InvariantCulture or StringComparison.InvariantCultureIgnoreCase as the comparisonType parameter.

下面的示例将执行两个不区分区域性的非语义字符串比较。The following example performs two culture-insensitive, non-linguistic string comparisons. 第一个比较区分大小写,而第二个比较不区分大小写。The first is case-sensitive, but the second is not.

using System;

public class CompareSample
{
    public static void Main()
    {
        string string1 = "file";
        string string2 = "FILE";
        int compareResult = 0;

        compareResult = String.Compare(string1, string2,
                                       StringComparison.Ordinal);
        Console.WriteLine("{0} comparison of '{1}' and '{2}': {3}",
                          StringComparison.Ordinal, string1, string2,
                          compareResult);

        compareResult = String.Compare(string1, string2,
                                       StringComparison.OrdinalIgnoreCase);
        Console.WriteLine("{0} comparison of '{1}' and '{2}': {3}",
                          StringComparison.OrdinalIgnoreCase, string1, string2,
                          compareResult);
    }
}
// The example displays the following output:
//    Ordinal comparison of 'file' and 'FILE': 32
//    OrdinalIgnoreCase comparison of 'file' and 'FILE': 0
Public Class CompareSample
    Public Shared Sub Main()
        Dim string1 As String = "file"
        Dim string2 As String = "FILE"
        Dim compareResult As Integer

        compareResult = String.Compare(string1, string2, _
                                       StringComparison.Ordinal)
        Console.WriteLine("{0} comparison of '{1}' and '{2}': {3}",
                          StringComparison.Ordinal, string1, string2,
                          compareResult)

        compareResult = String.Compare(string1, string2,
                                       StringComparison.OrdinalIgnoreCase)
        Console.WriteLine("{0} comparison of '{1}' and '{2}': {3}",
                          StringComparison.OrdinalIgnoreCase, string1, string2,
                          compareResult)
    End Sub
End Class
' The example displays the following output:
'    Ordinal comparison of 'file' and 'FILE': 32
'    OrdinalIgnoreCase comparison of 'file' and 'FILE': 0

可以下载排序权重表,这是一组文本文件,其中包含有关 Windows 操作系统排序和比较操作中所使用的字符权重的信息,也可以下载默认 Unicode 排序元素表,这是适用于 Linux 和 macOS 的排序权重表。You can download the Sorting Weight Tables, a set of text files that contain information on the character weights used in sorting and comparison operations for Windows operating systems, and the Default Unicode Collation Element Table, the sort weight table for Linux and macOS.

另请参阅See also