CompareInfo 類別

定義

實作區分文化特性 (Culture) 的字串比較的一組方法。

public ref class CompareInfo
public ref class CompareInfo sealed : System::Runtime::Serialization::IDeserializationCallback
public ref class CompareInfo : System::Runtime::Serialization::IDeserializationCallback
public class CompareInfo
public sealed class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
[System.Serializable]
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
type CompareInfo = class
type CompareInfo = class
    interface IDeserializationCallback
[<System.Serializable>]
type CompareInfo = class
    interface IDeserializationCallback
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CompareInfo = class
    interface IDeserializationCallback
Public Class CompareInfo
Public NotInheritable Class CompareInfo
Implements IDeserializationCallback
Public Class CompareInfo
Implements IDeserializationCallback
繼承
CompareInfo
屬性
實作

範例

下列範例顯示與 CultureInfo 物件相關聯的 物件如何影響 CompareInfo 字串比較。

using namespace System;
using namespace System::Text;
using namespace System::Globalization;

int main()
{
    array<String^>^ sign = gcnew array<String^> { "<", "=", ">" };

    // The code below demonstrates how strings compare
    // differently for different cultures.
    String^ s1 = "Coté"; 
    String^ s2 = "coté";
    String^ s3 = "côte";

    // Set sort order of strings for French in France.
    CompareInfo^ ci = (gcnew CultureInfo("fr-FR"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using fr-FR Compare of Coté = coté.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s1, s2, sign[ci->Compare(s1, s2, CompareOptions::IgnoreCase) + 1]);

    // Display the result using fr-FR Compare of coté > côte.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3, CompareOptions::None) + 1]);

    // Set sort order of strings for Japanese as spoken in Japan.
    ci = (gcnew CultureInfo("ja-JP"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using ja-JP Compare of coté < côte.
    Console::WriteLine("ja-JP Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3) + 1]);
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main(string[] args)
    {
        String[] sign = new String[] { "<", "=", ">" };

        // The code below demonstrates how strings compare
        // differently for different cultures.
        String s1 = "Coté", s2 = "coté", s3 = "côte";

        // Set sort order of strings for French in France.
        CompareInfo ci = new CultureInfo("fr-FR").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s1, s2, sign[ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1]);

        // Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3, CompareOptions.None) + 1]);

        // Set sort order of strings for Japanese as spoken in Japan.
        ci = new CultureInfo("ja-JP").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using ja-JP Compare of coté < côte.
        Console.WriteLine("ja-JP Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3) + 1]);
    }
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
Imports System.Text
Imports System.Globalization

NotInheritable Public Class App
    Shared Sub Main(ByVal args() As String) 
        Dim sign() As String = {"<", "=", ">"}
        
        ' The code below demonstrates how strings compare 
        ' differently for different cultures.
        Dim s1 As String = "Coté"
        Dim s2 As String = "coté"
        Dim s3 As String = "côte"
        
        ' Set sort order of strings for French in France.
        Dim ci As CompareInfo = New CultureInfo("fr-FR").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s1, s2, sign((ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1)))
        
        ' Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3, CompareOptions.None) + 1)))
        
        ' Set sort order of strings for Japanese as spoken in Japan.
        ci = New CultureInfo("ja-JP").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using ja-JP Compare of coté < côte. 
        Console.WriteLine("ja-JP Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3) + 1)))
    End Sub
End Class

' This code produces the following output.
' 
' The LCID for fr-FR is 1036.
' fr-FR Compare: Coté = coté
' fr-FR Compare: coté > côte
' The LCID for ja-JP is 1041.
' ja-JP Compare: coté < côte

備註

比較和排序資料的慣例會因文化特性而異。 例如,排序次序可能以注音或字元的視覺表示為基礎。 在東亞語言中,字元會依筆劃和語意的根基排序。 排序也取決於字母順序所使用的順序語言和文化特性。 例如,丹麥文的 "Æ" 字元在字母順序中排列在 "Z" 之後。 此外,比較可能會區分大小寫或區分大小寫,而大小寫規則也可能因文化特性而異。 類別 CompareInfo 負責維護此區分文化特性的字串比較資料,以及執行區分文化特性的字串作業。

一般而言,您不需要直接具現化 CompareInfo 物件,因為所有非序數位符串比較作業都會隱含地使用物件,包括呼叫 String.Compare 方法。 不過,如果您想要擷取 CompareInfo 物件,您可以使用下列其中一種方式來執行此動作:

忽略的搜尋值

字元集包含可忽略的字元,這些字元在執行語言或區分文化特性的比較時不列入考慮。 比較方法,例如 IndexOfLastIndexOf 不會在執行區分文化特性的比較時考慮這類字元。 可忽略的字元包括:

  • String.Empty. 區分文化特性的比較方法一律會在搜尋字串的開頭 (索引零) 找到空字串。

  • 字元或字串,其中包含因為比較選項而未在作業中考慮之字碼點的字元,特別是 CompareOptions.IgnoreNonSpaceCompareOptions.IgnoreSymbols 選項會產生搜尋,其中會忽略符號和非步調合併字元。

  • 具有沒有語言重要性之字碼點的字串。 例如,在區分文化特性的字串比較中,一律會忽略虛連字號 (U+00AD) 。

安全性考量

如果安全性決策取決於字串比較或大小寫變更,您應該使用 InvariantCulture 屬性來確保行為一致,不論作業系統的文化特性設定為何。

注意

可能的話,您應該使用具有 型 CompareOptions 別參數的字串比較方法,以指定預期的比較類型。 一般規則是,使用語言選項 (使用目前的文化特性) 來比較使用者介面中顯示的字串,並指定 OrdinalOrdinalIgnoreCase 進行安全性比較。

屬性

LCID

為目前的 CompareInfo 取得正確格式的文化特性識別項。

Name

取得由此 CompareInfo 物件進行排序作業所使用之文化特性的名稱。

Version

取得用於比較和排序字串的 Unicode 版本的相關資訊。

方法

Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

比較兩個唯讀字元範圍。

Compare(String, Int32, Int32, String, Int32, Int32)

將一個字串的區段與另一個字串的區段相比較。

Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions)

使用指定的 CompareOptions 值,將一個字串的區段與另一個字串的區段相比較。

Compare(String, Int32, String, Int32)

將字串的結尾區段與另一個字串的結尾區段相比較。

Compare(String, Int32, String, Int32, CompareOptions)

使用指定的 CompareOptions 值,將字串的結尾區段與另一個字串的結尾區段相比較。

Compare(String, String)

比較兩個字串。

Compare(String, String, CompareOptions)

使用指定的 CompareOptions 值來比較兩個字串。

Equals(Object)

判斷指定的物件是否等於目前的 CompareInfo 物件。

GetCompareInfo(Int32)

初始化新的 CompareInfo 物件,這個物件與具有所指定識別項的文化特性相關聯。

GetCompareInfo(Int32, Assembly)

初始化新的 CompareInfo 物件,這個物件與所指定文化特性相關聯,而且使用所指定 Assembly 中的字串比較方法。

GetCompareInfo(String)

初始化新的 CompareInfo 物件,這個物件與具有所指定名稱的文化特性相關聯。

GetCompareInfo(String, Assembly)

初始化新的 CompareInfo 物件,這個物件與所指定文化特性相關聯,而且使用所指定 Assembly 中的字串比較方法。

GetHashCode()

做為目前 CompareInfo 的雜湊函式,用於雜湊演算法和資料結構,例如雜湊資料表。

GetHashCode(ReadOnlySpan<Char>, CompareOptions)

根據所指定比較選項取得字元範圍的雜湊碼。

GetHashCode(String, CompareOptions)

根據指定的比較選項,取得字串的雜湊碼。

GetSortKey(ReadOnlySpan<Char>, Span<Byte>, CompareOptions)

計算指定輸入的排序鍵。

GetSortKey(String)

取得指定之字串的排序鍵。

GetSortKey(String, CompareOptions)

使用指定的 SortKey 值,取得指定之字串的 CompareOptions 物件。

GetSortKeyLength(ReadOnlySpan<Char>, CompareOptions)

取得要從指定輸入產生的排序鍵位元組總數。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

在所指定唯讀字元範圍中搜尋第一個出現的子字串。

IndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

在來源字串中搜尋第一個出現的子字串。

IndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

在所指定唯讀字元範圍中搜尋第一個出現的 Rune

IndexOf(String, Char)

搜尋指定的字元,並傳回整個來源字串內第一個相符項目的以零為起始的索引。

IndexOf(String, Char, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的字元,並傳回整個來源字串內第一個相符項目的以零起始的索引。

IndexOf(String, Char, Int32)

搜尋指定的字元,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零為起始的索引。

IndexOf(String, Char, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零起始的索引。

IndexOf(String, Char, Int32, Int32)

搜尋指定的字元,並傳回來源字串區段 (起始於指定索引並且含有指定的項目數) 內第一個相符項目的以零為起始的索引。

IndexOf(String, Char, Int32, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回第一個相符項目 (在來源字串中起始於指定索引且含有指定項目數的區段內) 的以零起始的索引。

IndexOf(String, String)

搜尋指定的子字串,並傳回來源字串內第一個相符項目的以零為起始的索引。

IndexOf(String, String, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的子字串,並傳回整個來源字串內第一個相符項目的以零起始的索引。

IndexOf(String, String, Int32)

搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零為起始的索引。

IndexOf(String, String, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零起始的索引。

IndexOf(String, String, Int32, Int32)

搜尋指定的子字串,並傳回來源字串的區段 (起始於指定索引且含有指定項目數) 內第一個相符項目以零為起始的索引。

IndexOf(String, String, Int32, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中起始於指定索引且含有指定項目數的區段內) 的以零起始的索引。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

判斷唯讀字元範圍是否以特定字首開始。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

判斷字串是否以特定字首開始。

IsPrefix(String, String)

判斷指定的來源字串是否以指定字首開始。

IsPrefix(String, String, CompareOptions)

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字首開始。

IsSortable(Char)

指出指定的 Unicode 字元是否可排序。

IsSortable(ReadOnlySpan<Char>)

指出指定的 Unicode 唯讀字元範圍是否可排序。

IsSortable(Rune)

指出指定的 Rune 是否可排序。

IsSortable(String)

指出指定的 Unicode 字串是否可排序。

IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

判斷唯讀字元範圍是否以特定字尾結束。

IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

判斷字串是否以特定字尾結束。

IsSuffix(String, String)

判斷指定的來源字串是否以指定字尾結束。

IsSuffix(String, String, CompareOptions)

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字尾結束。

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

在所指定唯讀字元範圍中搜尋最後一個出現的子字串。

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

在來源字串中搜尋最後一個出現的子字串。

LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

在所指定唯讀字元範圍中搜尋最後一個出現的 Rune

LastIndexOf(String, Char)

搜尋指定的字元,並傳回整個來源字串內最後一個相符項目的以零為起始的索引。

LastIndexOf(String, Char, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的字元,並傳回整個來源字串內最後一個相符項目的以零起始的索引。

LastIndexOf(String, Char, Int32)

搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零為起始的索引。

LastIndexOf(String, Char, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零起始的索引。

LastIndexOf(String, Char, Int32, Int32)

搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零為起始的索引。

LastIndexOf(String, Char, Int32, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零起始的索引。

LastIndexOf(String, String)

搜尋指定的子字串,並傳回整個來源字串中最後一個相符項目的以零為起始的索引。

LastIndexOf(String, String, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的子字串,並傳回整個來源字串內最後一個相符項目的以零起始的索引。

LastIndexOf(String, String, Int32)

搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零為起始的索引。

LastIndexOf(String, String, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回在來源字串的區段 (從字串開頭延伸至指定索引) 內最後一個相符項目的以零起始的索引。

LastIndexOf(String, String, Int32, Int32)

搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零為起始的索引。

LastIndexOf(String, String, Int32, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零起始的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回表示目前 CompareInfo 物件的字串。

明確介面實作

IDeserializationCallback.OnDeserialization(Object)

執行於整個物件 Graph 已經還原序列化時。

擴充方法

GetStringComparer(CompareInfo, CompareOptions)

根據所指定之 CompareInfo 物件的區分文化特性字串比較規則,傳回 StringComparer 物件。

適用於

另請參閱