執行不區分文化特性的字串比較

根據預設,String.Compare 方法會執行區分文化特性和區分大小寫的比較。 這個方法也包含幾個多載,這些多載會提供 culture 參數 (讓您指定要使用的文化特性) 及 comparisonType 參數 (讓您指定要使用的比較規則)。 呼叫這些方法 (而不是預設多載) 會消除有關特定方法呼叫中使用之規則的任何模稜兩可情況,而且可以釐清特定比較是否區分文化特性。

注意

String.CompareTo 方法的兩個多載都會執行區分文化特性和區分大小寫的比較,您不能使用這個方法來執行不區分文化特性的比較。 為了讓程式碼更清楚,我們建議您改用 String.Compare 方法。

若要進行區分文化特性作業,請指定 StringComparison.CurrentCultureStringComparison.CurrentCultureIgnoreCase 列舉值當做 comparisonType 參數。 如果您想要使用目前文化特性以外的指定文化特性來執行區分文化特性的比較,請指定代表該文化特性的 CultureInfo 物件當做 culture 參數。

String.Compare 方法支援的不區分文化特性的字串比較為語言式 (根據不因國別而異的文化特性排序慣例) 或是非語言式 (根據字串中字元的序數值)。 大多數不區分文化特性的字串比較都是非語言式。 若要進行這些比較,請指定 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase 列舉值當做 comparisonType 參數。 例如,如果安全性決策 (例如使用者名稱或密碼比較) 是依據字串比較的結果,此作業應該不區分文化特性而且為非語言式,以確保結果不受特定文化特性或語言的慣例所影響

如果您想要以一致的方式處理多個文化特性中語言相關的字串,請使用不區分文化特性的語言字串比較。 例如,如果應用程式顯示的字詞會使用清單方塊中的多個字元集,您可能會想要以相同的順序顯示字詞,不論目前的文化特性為何。 如果是不區分文化特性的語言比較,則 .NET 會定義根據英文語言慣例的非變異文化特性。 若要執行不區分文化特性的語言作業,請指定 StringComparison.InvariantCultureStringComparison.InvariantCultureIgnoreCase 當做 comparisonType 參數。

下列範例執行兩個不區分文化特性的非語言式字串比較。 第一個比較區分大小寫,第二個比較則不區分。

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 的排序權數資料表)。

另請參閱