CompareInfo 類別

定義

實作區分文化特性 (Culture) 的字串比較的一組方法。Implements a set of methods for culture-sensitive string comparisons.

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

範例

下列範例顯示CultureInfo與物件相關CompareInfo聯的物件如何影響字串比較。The following example shows how the CompareInfo object associated with a CultureInfo object affects string comparison.

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

備註

比較和排序資料的慣例, 會因文化特性而異。Conventions for comparing and sorting data vary from culture to culture. 例如, 排序次序可能是根據拼音或字元的視覺標記法。For example, sort order may be based on phonetics or on the visual representation of characters. 在東亞語言中, 字元是以筆劃和開方的根式排序。In East Asian languages, characters are sorted by the stroke and radical of ideographs. 排序也取決於用於字母的訂單語言和文化特性。Sorting also depends on the order languages and cultures use for the alphabet. 例如,丹麥文的 "Æ" 字元在字母順序中排列在 "Z" 之後。For example, the Danish language has an "Æ" character that it sorts after "Z" in the alphabet. 此外, 比較可能會區分大小寫或不區分大小寫, 而且大小寫規則可能也會因文化特性而異。In addition, comparisons may be case-sensitive or case-insensitive, and casing rules may also differ by culture. CompareInfo類別負責維護此區分文化特性的字串比較資料, 以及執行區分文化特性的字串作業。The CompareInfo class is responsible for maintaining this culture-sensitive string comparison data and for performing culture-sensitive string operations.

一般而言, 您不需要直接具現化CompareInfo物件, 因為所有非序數位符串比較作業 (包括對方法的String.Compare呼叫) 都會隱含使用其中一個。Typically, you do not have to instantiate a CompareInfo object directly, because one is used implicitly by all non-ordinal string comparison operations, including calls to the String.Compare method. 不過, 如果您想要取出CompareInfo物件, 您可以使用下列其中一種方式來執行它:However, if you do want to retrieve a CompareInfo object, you can do it in one of these ways:

忽略的搜尋值Ignored search values

字元集包含可忽略的字元,這些字元在執行語言或區分文化特性的比較時不列入考慮。Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. 比較方法 (例如IndexOfLastIndexOf ) 在執行區分文化特性的比較時, 不會考慮這類字元。Comparison methods such as IndexOf and LastIndexOf do not consider such characters when they perform a culture-sensitive comparison. 忽略的字元包括:Ignorable characters include:

  • String.Empty.String.Empty. 區分文化特性的比較方法一律會在所搜尋字串的開頭 (索引零) 上尋找空字串。Culture-sensitive comparison methods will always find an empty string at the beginning (index zero) of the string being searched.

  • 字元或字串, 其中包含的字元與因比較選項而未在作業中考慮的程式碼點, 特別CompareOptions.IgnoreNonSpace是和CompareOptions.IgnoreSymbols選項會產生搜尋, 其中的符號和非間距結合字元會被忽略。A character or string consisting of characters with code points that are not considered in the operation because of comparison options, In particular, the CompareOptions.IgnoreNonSpace and CompareOptions.IgnoreSymbols options produce searches in which symbols and nonspacing combining characters are ignored.

  • 包含沒有語言意義之程式碼點的字串。A string with code points that have no linguistic significance. 例如, 在區分文化特性的字串比較中, 一定會忽略軟連字號 (U + 00AD)。For example, a soft hyphen (U+00AD) is always ignored in a culture-sensitive string comparison.

安全性考量Security considerations

如果安全性決策取決於字串比較或大小寫變更, 您應該使用InvariantCulture屬性來確保行為一致, 不論作業系統的文化特性設定為何。If a security decision depends on a string comparison or a case change, you should use the InvariantCulture property to ensure that the behavior is consistent, regardless of the culture settings of the operating system.

注意

可能的話, 您應該使用具有型CompareOptions別參數的字串比較方法, 來指定預期的比較種類。When possible, you should use string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. 一般的規則是使用語言選項 (使用目前的文化特性) 來比較在使用者介面中顯示的字串, OrdinalOrdinalIgnoreCase針對安全性比較指定或。As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons.

屬性

LCID

為目前的 CompareInfo 取得正確格式的文化特性識別項。Gets the properly formed culture identifier for the current CompareInfo.

Name

取得由此 CompareInfo 物件進行排序作業所使用之文化特性的名稱。Gets the name of the culture used for sorting operations by this CompareInfo object.

Version

取得用於比較和排序字串的 Unicode 版本的相關資訊。Gets information about the version of Unicode used for comparing and sorting strings.

方法

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

將一個字串的區段與另一個字串的區段相比較。Compares a section of one string with a section of another string.

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

使用指定的 CompareOptions 值,將一個字串的區段與另一個字串的區段相比較。Compares a section of one string with a section of another string using the specified CompareOptions value.

Compare(String, Int32, String, Int32)

將字串的結尾區段與另一個字串的結尾區段相比較。Compares the end section of a string with the end section of another string.

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

使用指定的 CompareOptions 值,將字串的結尾區段與另一個字串的結尾區段相比較。Compares the end section of a string with the end section of another string using the specified CompareOptions value.

Compare(String, String)

比較兩個字串。Compares two strings.

Compare(String, String, CompareOptions)

使用指定的 CompareOptions 值來比較兩個字串。Compares two strings using the specified CompareOptions value.

Equals(Object)

判斷指定的物件是否等於目前的 CompareInfo 物件。Determines whether the specified object is equal to the current CompareInfo object.

GetCompareInfo(Int32)

初始化新的 CompareInfo 物件,這個物件與具有所指定識別項的文化特性相關聯。Initializes a new CompareInfo object that is associated with the culture with the specified identifier.

GetCompareInfo(Int32, Assembly)

初始化新的 CompareInfo 物件,這個物件與所指定文化特性相關聯,而且使用所指定 Assembly 中的字串比較方法。Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

GetCompareInfo(String)

初始化新的 CompareInfo 物件,這個物件與具有所指定名稱的文化特性相關聯。Initializes a new CompareInfo object that is associated with the culture with the specified name.

GetCompareInfo(String, Assembly)

初始化新的 CompareInfo 物件,這個物件與所指定文化特性相關聯,而且使用所指定 Assembly 中的字串比較方法。Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

GetHashCode()

做為目前 CompareInfo 的雜湊函式,用於雜湊演算法和資料結構,例如雜湊資料表。Serves as a hash function for the current CompareInfo for hashing algorithms and data structures, such as a hash table.

GetHashCode(ReadOnlySpan<Char>, CompareOptions)

根據所指定比較選項取得字元範圍的雜湊碼。Gets the hash code for a character span based on specified comparison options.

GetHashCode(String, CompareOptions)

根據指定的比較選項,取得字串的雜湊碼。Gets the hash code for a string based on specified comparison options.

GetSortKey(String)

取得指定之字串的排序鍵。Gets the sort key for the specified string.

GetSortKey(String, CompareOptions)

使用指定的 SortKey 值,取得指定之字串的 CompareOptions 物件。Gets a SortKey object for the specified string using the specified CompareOptions value.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(String, Char)

搜尋指定的字元,並傳回整個來源字串內第一個相符項目的以零為起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.

IndexOf(String, Char, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的字元,並傳回整個來源字串內第一個相符項目的以零起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string using the specified CompareOptions value.

IndexOf(String, Char, Int32)

搜尋指定的字元,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零為起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string.

IndexOf(String, Char, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.

IndexOf(String, Char, Int32, Int32)

搜尋指定的字元,並傳回來源字串區段 (起始於指定索引並且含有指定的項目數) 內第一個相符項目的以零為起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements.

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

使用指定 CompareOptions 值,搜尋指定的字元,並傳回第一個相符項目 (在來源字串中起始於指定索引且含有指定項目數的區段內) 的以零起始的索引。Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements using the specified CompareOptions value.

IndexOf(String, String)

搜尋指定的子字串,並傳回來源字串內第一個相符項目的以零為起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string.

IndexOf(String, String, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的子字串,並傳回整個來源字串內第一個相符項目的以零起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the entire source string using the specified CompareOptions value.

IndexOf(String, String, Int32)

搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零為起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string.

IndexOf(String, String, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中從指定索引延伸至字串結尾的區段內) 的以零起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.

IndexOf(String, String, Int32, Int32)

搜尋指定的子字串,並傳回來源字串的區段 (起始於指定索引且含有指定項目數) 內第一個相符項目以零為起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements.

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

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回第一個相符項目 (在來源字串中起始於指定索引且含有指定項目數的區段內) 的以零起始的索引。Searches for the specified substring and returns the zero-based index of the first occurrence within the section of the source string that starts at the specified index and contains the specified number of elements using the specified CompareOptions value.

IsPrefix(String, String)

判斷指定的來源字串是否以指定字首開始。Determines whether the specified source string starts with the specified prefix.

IsPrefix(String, String, CompareOptions)

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字首開始。Determines whether the specified source string starts with the specified prefix using the specified CompareOptions value.

IsSortable(Char)

指出指定的 Unicode 字元是否可排序。Indicates whether a specified Unicode character is sortable.

IsSortable(String)

指出指定的 Unicode 字串是否可排序。Indicates whether a specified Unicode string is sortable.

IsSuffix(String, String)

判斷指定的來源字串是否以指定字尾結束。Determines whether the specified source string ends with the specified suffix.

IsSuffix(String, String, CompareOptions)

使用指定的 CompareOptions 值,判斷指定的來源字串是否以指定字尾結束。Determines whether the specified source string ends with the specified suffix using the specified CompareOptions value.

LastIndexOf(String, Char)

搜尋指定的字元,並傳回整個來源字串內最後一個相符項目的以零為起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.

LastIndexOf(String, Char, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的字元,並傳回整個來源字串內最後一個相符項目的以零起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.

LastIndexOf(String, Char, Int32)

搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零為起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index.

LastIndexOf(String, Char, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.

LastIndexOf(String, Char, Int32, Int32)

搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零為起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index.

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

使用指定 CompareOptions 值,搜尋指定的字元,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零起始的索引。Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index using the specified CompareOptions value.

LastIndexOf(String, String)

搜尋指定的子字串,並傳回整個來源字串中最後一個相符項目的以零為起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string.

LastIndexOf(String, String, CompareOptions)

使用指定的 CompareOptions 值,搜尋指定的子字串,並傳回整個來源字串內最後一個相符項目的以零起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.

LastIndexOf(String, String, Int32)

搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中從字串開頭延伸至指定索引的區段內) 的以零為起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index.

LastIndexOf(String, String, Int32, CompareOptions)

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回在來源字串的區段 (從字串開頭延伸至指定索引) 內最後一個相符項目的以零起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.

LastIndexOf(String, String, Int32, Int32)

搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零為起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index.

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

使用指定 CompareOptions 值,搜尋指定的子字串,並傳回最後一個相符項目 (在來源字串中含有指定項目數且結束於指定索引的區段內) 的以零起始的索引。Searches for the specified substring and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index using the specified CompareOptions value.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回表示目前 CompareInfo 物件的字串。Returns a string that represents the current CompareInfo object.

明確介面實作

IDeserializationCallback.OnDeserialization(Object)

執行於整個物件 Graph 已經還原序列化時。Runs when the entire object graph has been deserialized.

擴充方法

GetStringComparer(CompareInfo, CompareOptions)

根據所指定之 CompareInfo 物件的區分文化特性字串比較規則,傳回 StringComparer 物件。Returns a StringComparer object based on the culture-sensitive string comparison rules of a specified CompareInfo object.

適用於

另請參閱