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

指示字串比較必須忽略假名類型。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 Standard (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 排序和字串排序序數排序。.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.

適用於

另請參閱