CompareOptions CompareOptions CompareOptions CompareOptions Enum

定义

定义要用于 CompareInfo 的字符串比较选项。Defines the string comparison options to use with CompareInfo.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class CompareOptions
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum CompareOptions
type CompareOptions = 
Public Enum CompareOptions
继承
CompareOptionsCompareOptionsCompareOptionsCompareOptions
属性

字段

IgnoreCase IgnoreCase IgnoreCase IgnoreCase 1

指示字符串比较必须忽略大小写。Indicates that the string comparison must ignore case.

IgnoreKanaType IgnoreKanaType IgnoreKanaType IgnoreKanaType 8

指示字符串比较必须忽略 Kana 类型。Indicates that the string comparison must ignore the Kana type. 假名类型是指日语平假名和片假名字符,它们表示日语中的语音。Kana type refers to Japanese hiragana and katakana characters, which represent phonetic sounds in the Japanese language. 平假名用于表示日语自有的短语和字词,而片假名用于表示从其他语言借用的字词,如“computer”或“Internet”。Hiragana is used for native Japanese expressions and words, while katakana is used for words borrowed from other languages, such as "computer" or "Internet". 语音既可以用平假名也可以用片假名表示。A phonetic sound can be expressed in both hiragana and katakana. 如果选择该值,则认为一个语音的平假名字符等于同一语音的片假名字符。If this value is selected, the hiragana character for one sound is considered equal to the katakana character for the same sound.

IgnoreNonSpace IgnoreNonSpace IgnoreNonSpace IgnoreNonSpace 2

指示字符串比较必须忽略不占空间的组合字符,比如音调符号。Indicates that the string comparison must ignore nonspacing combining characters, such as diacritics. Unicode 标准将组合字符定义为与基字符组合起来产生新字符的字符。The Unicode Standard defines combining characters as characters that are combined with base characters to produce a new character. 不占空间的组合字符在呈现时其本身不占用空间位置。Nonspacing combining characters do not occupy a spacing position by themselves when rendered.

IgnoreSymbols IgnoreSymbols IgnoreSymbols IgnoreSymbols 4

指示字符串比较必须忽略符号,如空格字符、标点、货币符号、百分号、数学符号、& 号等等。Indicates that the string comparison must ignore symbols, such as white-space characters, punctuation, currency symbols, the percent sign, mathematical symbols, the ampersand, and so on.

IgnoreWidth IgnoreWidth IgnoreWidth IgnoreWidth 16

指示字符串比较必须忽略字符宽度。Indicates that the string comparison must ignore the character width. 例如,日语片假名字符可以写为全角或半角形式。For example, Japanese katakana characters can be written as full-width or half-width. 如果选择此值,则认为片假名字符的全角形式等同于半角形式。If this value is selected, the katakana characters written as full-width are considered equal to the same characters written as half-width.

None None None None 0

指定字符串比较的默认选项设置。Indicates the default option settings for string comparisons.

Ordinal Ordinal Ordinal Ordinal 1073741824

指示必须使用字符串的连续 Unicode UTF-16 编码值进行字符串比较(使用代码单元进行代码单元比较),这样可以提高比较速度,但不能区分区域性。Indicates that the string comparison must use successive Unicode UTF-16 encoded values of the string (code unit by code unit comparison), leading to a fast comparison but one that is culture-insensitive. 如果 XXXX16 小于 YYYY16,则以“XXXX16”代码单元开头的字符串位于以“YYYY16”代码单元开头的字符串之前。A string starting with a code unit XXXX16 comes before a string starting with YYYY16, if XXXX16 is less than YYYY16. 此值必须单独使用,而不能与其他 CompareOptions 值组合在一起。This value cannot be combined with other CompareOptions values and must be used alone.

OrdinalIgnoreCase OrdinalIgnoreCase OrdinalIgnoreCase OrdinalIgnoreCase 268435456

字符串比较必须忽略大小写,然后执行序号比较。String comparison must ignore case, then perform an ordinal comparison. 此方法相当于先使用固定区域性将字符串转换为大写,然后再对结果执行序号比较。This technique is equivalent to converting the string to uppercase using the invariant culture and then performing an ordinal comparison on the result.

StringSort StringSort StringSort StringSort 536870912

指示字符串比较必须使用字符串排序算法。Indicates that the string comparison must use the string sort algorithm. 在字符串排序中,连字符、撇号以及其他非字母数字符号都排在字母数字字符之前。In a string sort, the hyphen and the apostrophe, as well as other nonalphanumeric symbols, come before alphanumeric characters.

示例

下面的代码示例演示如何使用 StringSort 进行排序不同于而无需 StringSort 排序。The following code example shows how sorting with StringSort differs from sorting without StringSort.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

// __gc public class SamplesCompareOptions {
ref class MyStringComparer: public IComparer
{
public:

   // Constructs a comparer using the specified CompareOptions.
   CompareInfo^ myComp;
   CompareOptions myOptions;
   MyStringComparer( CompareInfo^ cmpi, CompareOptions options )
      : myComp( cmpi ), myOptions( options )
   {}

   // Compares strings with the CompareOptions specified in the constructor.
   virtual int Compare( Object^ a, Object^ b )
   {
      if ( a == b )
            return 0;

      if ( a == nullptr )
            return  -1;

      if ( b == nullptr )
            return 1;

      String^ sa = dynamic_cast<String^>(a);
      String^ sb = dynamic_cast<String^>(b);
      if ( sa != nullptr && sb != nullptr )
            return myComp->Compare( sa, sb, myOptions );

      throw gcnew ArgumentException( "a and b should be strings." );
   }
};

int main()
{
   
   // Creates and initializes an array of strings to sort.
   array<String^>^myArr = {"cant","bill's","coop","cannot","billet","can't","con","bills","co-op"};
   Console::WriteLine( "\nInitially, " );
   IEnumerator^ myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ myStr = safe_cast<String^>(myEnum->Current);
      Console::WriteLine( myStr );
   }

   
   // Creates and initializes a Comparer to use.
   //CultureInfo* myCI = new CultureInfo(S"en-US", false);
   MyStringComparer^ myComp = gcnew MyStringComparer( CompareInfo::GetCompareInfo( "en-US" ),CompareOptions::None );
   
   // Sorts the array without StringSort.
   Array::Sort( myArr, myComp );
   Console::WriteLine( "\nAfter sorting without CompareOptions::StringSort:" );
   myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ myStr = safe_cast<String^>(myEnum->Current);
      Console::WriteLine( myStr );
   }

   
   // Sorts the array with StringSort.
   myComp = gcnew MyStringComparer( CompareInfo::GetCompareInfo( "en-US" ),CompareOptions::StringSort );
   Array::Sort( myArr, myComp );
   Console::WriteLine( "\nAfter sorting with CompareOptions::StringSort:" );
   myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ myStr = safe_cast<String^>(myEnum->Current);
      Console::WriteLine( myStr );
   }
}

/*
This code produces the following output.

Initially,
cant
bill's
coop
cannot
billet
can't
con
bills
co-op

After sorting without CompareOptions::StringSort:
billet
bills
bill's
cannot
cant
can't
con
coop
co-op

After sorting with CompareOptions::StringSort:
bill's
billet
bills
can't
cannot
cant
co-op
con
coop
*/
using System;
using System.Collections;
using System.Globalization;


public class SamplesCompareOptions  {

   private class MyStringComparer: IComparer {
      private CompareInfo myComp;   
      private CompareOptions myOptions = CompareOptions.None;

      // Constructs a comparer using the specified CompareOptions.
      public MyStringComparer( CompareInfo cmpi, CompareOptions options )  {
         myComp = cmpi;
         this.myOptions = options;
      }

      // Compares strings with the CompareOptions specified in the constructor.
      public int Compare(Object a, Object b) {
         if (a == b) return 0;
         if (a == null) return -1;
         if (b == null) return 1;

         String sa = a as String;
         String sb = b as String;
         if (sa != null && sb != null)
            return myComp.Compare(sa, sb, myOptions);
         throw new ArgumentException("a and b should be strings.");

      }
   }
   
   public static void Main()  {

      // Creates and initializes an array of strings to sort.
      String[] myArr = new String[9] { "cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op" };
      Console.WriteLine( "\nInitially," );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Creates and initializes a Comparer to use.
      //CultureInfo myCI = new CultureInfo( "en-US", false );
      MyStringComparer myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);

      // Sorts the array without StringSort.
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting without CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Sorts the array with StringSort.
      myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting with CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

   }

}

/*
This code produces the following output.

Initially,
cant
bill's
coop
cannot
billet
can't
con
bills
co-op

After sorting without CompareOptions.StringSort:
billet
bills
bill's
cannot
cant
can't
con
coop
co-op

After sorting with CompareOptions.StringSort:
bill's
billet
bills
can't
cannot
cant
co-op
con
coop

*/
Imports System
Imports System.Collections
Imports System.Globalization

Public Class SamplesCompareOptions

   Private Class MyStringComparer
      Implements IComparer

      Private myComp As CompareInfo
      Private myOptions As CompareOptions = CompareOptions.None
      
      ' Constructs a comparer using the specified CompareOptions.
      Public Sub New(cmpi As CompareInfo, options As CompareOptions)
         myComp = cmpi
         Me.myOptions = options
      End Sub 'New
      
      ' Compares strings with the CompareOptions specified in the constructor.
      Public Function Compare(a As [Object], b As [Object]) As Integer Implements IComparer.Compare
         If a = b Then
            Return 0
         End If
         If a Is Nothing Then
            Return - 1
         End If
         If b Is Nothing Then
            Return 1
         End If 

         Dim sa As [String] = a
         Dim sb As [String] = b
         If Not (sa Is Nothing) And Not (sb Is Nothing) Then
            Return myComp.Compare(sa, sb, myOptions)
         End If
         Throw New ArgumentException("a and b should be strings.")

      End Function 'Compare 

   End Class 'MyStringComparer


   Public Shared Sub Main()
      
      ' Creates and initializes an array of strings to sort.
      Dim myArr() As [String] = {"cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op"}
      Console.WriteLine()
      Console.WriteLine("Initially,")
      Dim myStr As [String]
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Creates and initializes a Comparer to use.
      'CultureInfo myCI = new CultureInfo( "en-US", false );
      Dim myComp As New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None)
      
      ' Sorts the array without StringSort.
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting without CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Sorts the array with StringSort.
      myComp = New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort)
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting with CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

   End Sub 'Main

End Class 'SamplesCompareOptions 


'This code produces the following output.
'
'Initially,
'cant
'bill's
'coop
'cannot
'billet
'can't
'con
'bills
'co-op
'
'After sorting without CompareOptions.StringSort:
'billet
'bills
'bill's
'cannot
'cant
'can't
'con
'coop
'co-op
'
'After sorting with CompareOptions.StringSort:
'bill's
'billet
'bills
'can't
'cannot
'cant
'co-op
'con
'coop

注解

这些选项指明区分大小写或需要忽略的字符类型。These options denote case sensitivity or necessity to ignore types of characters.

.NET 使用三种不同方式进行排序的: word sort、 字符串排序和序号排序。.NET uses three distinct ways of sorting: word sort, string sort, and ordinal sort. 单词排序执行区分区域性的比较的字符串。Word sort performs a culture-sensitive comparison of strings. 某些非字母数字字符可能会有特殊的权重分配给他们。Certain nonalphanumeric characters might have special weights assigned to them. 例如,连字符 ("-") 可能具有很小的权重分配给它,以便"coop"和"co-op"显示彼此排序列表中。For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. 不同的是没有特殊的情况下,字符串排序是类似于单词排序。String sort is similar to word sort, except that there are no special cases. 因此,所有非字母数字符号排在所有字母数字字符之前。Therefore, all nonalphanumeric symbols come before all alphanumeric characters. 序号排序比较字符串的每个元素的 Unicode 值的字符串。Ordinal sort compares strings based on the Unicode values of each element of the string. 包含有关在 Windows 操作系统的排序和比较操作中使用的字符权重的信息的文本文件的组可供下载,请参阅排序权重表For a downloadable set of text files that contain information on the character weights used in sorting and comparison operations for Windows operating systems, see Sorting Weight Tables. 适用于 Linux 和 macOS 的排序权重表,请参阅默认 Unicode 排序规则元素表For the sort weight table for Linux and macOS, see the Default Unicode Collation Element Table. Linux 和 macOS 上的特定排序权重表版本取决于系统上安装的 International Components for Unicode 库的版本。The specific version of the sort weight table on Linux and macOS depends on the version of the International Components for Unicode libraries installed on the system. 有关 ICU 版本及它们所实现的 Unicode 版本的信息,请参阅下载 ICUFor information on ICU versions and the Unicode versions that they implement, see Downloading ICU.

StringSort值仅可用于CompareInfo.CompareCompareInfo.GetSortKeyThe StringSort value can only be used with CompareInfo.Compare and CompareInfo.GetSortKey. ArgumentException 如果 StringSort 值一起使用时将引发CompareInfo.IsPrefixCompareInfo.IsSuffixCompareInfo.IndexOf,或CompareInfo.LastIndexOfArgumentException is thrown if the StringSort value is used with CompareInfo.IsPrefix, CompareInfo.IsSuffix, CompareInfo.IndexOf, or CompareInfo.LastIndexOf.

备注

如果可能,应使用接受的字符串比较方法CompareOptions值,以指定的预期的比较类型。When possible, you should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. 作为一般规则,面向用户的比较提供最佳服务时应指定比较的安全 (使用当前区域性) 的语言选项的用法OrdinalOrdinalIgnoreCaseAs a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase.

有关区分区域性的排序说明Notes on culture-sensitive sorts

备注

.NET Core 仅在 Linux 和 macOS 系统上运行: C 和 Posix 区域性的排序规则行为始终区分大小写,因为这些区域性不使用预期的 Unicode 排序规则顺序。.NET Core running on Linux and macOS systems only: The collation behavior for the C and Posix cultures is always case-sensitive because these cultures do not use the expected Unicode collation order. 建议使用除 C 或 Posix 以外的区域性执行区分区域性但不区分大小写的排序操作。We recommend that you use a culture other than C or Posix for performing culture-sensitive, case-insensitive sorting operations.

适用于

另请参阅