CompareInfo 类

定义

实现用于区分区域性的字符串的一组方法。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
属性
实现

示例

下面的示例演示CompareInfo CultureInfo与对象关联的对象如何影响字符串比较。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.EmptyString.Empty. 区分区域性的比较方法将始终在要搜索的字符串的开头(索引为零)查找空字符串。Culture-sensitive comparison methods will always find an empty string at the beginning (index zero) of the string being searched.

  • 字符或由多个字符和运算中未考虑的码位(因为比较选项)构成的字符串,具体而言,CompareOptions.IgnoreNonSpaceCompareOptions.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. 作为一般规则, 请使用语言选项 (使用当前区域性) 比较用户界面中显示的字符串, 并为安全Ordinal比较OrdinalIgnoreCase指定或。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)

在整个对象图形已经反序列化时运行。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.

适用于

另请参阅