CompareOptions Enumeration

Definition

Definiert die mit CompareInfo zu verwendenden Optionen für den Zeichenfolgenvergleich.

Diese Enumeration unterstützt eine bitweise Kombination ihrer Memberwerte.

public enum class CompareOptions
[System.Flags]
public enum CompareOptions
[System.Flags]
[System.Serializable]
public enum CompareOptions
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CompareOptions
[<System.Flags>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CompareOptions = 
Public Enum CompareOptions
Vererbung
CompareOptions
Attribute

Felder

IgnoreCase 1

Gibt an, dass beim Zeichenfolgenvergleich die Groß- und Kleinschreibung nicht beachtet wird.

IgnoreKanaType 8

Gibt an, dass beim Zeichenfolgenvergleich Zeichen vom Typ Kana ignoriert werden. Der Kana-Zeichentyp bezieht sich auf die japanischen Hiragana- und Katakana-Schriftzeichen, die im Japanischen phonetische Laute darstellen. Hiragana wird für japanische Ausdrücke und Wörter verwendet, während Katakana für Lehnwörter aus anderen Sprachen, z. B. "Computer" oder "Internet", verwendet wird. Ein phonetischer Laut kann sowohl in Hiragana als auch in Katakana dargestellt werden. Wenn dieser Wert ausgewählt ist, wird das Hiragana-Zeichen für einen Laut als gleichwertig mit dem Katakana-Zeichen für denselben Laut betrachtet.

IgnoreNonSpace 2

Gibt an, dass bei Zeichenfolgenvergleichen Kombinationszeichen ohne horizontalen Vorschub, z. B. diakritische Zeichen, ignoriert werden. Der Unicode-Standard definiert Kombinationszeichen als Zeichen, die mit Basiszeichen kombiniert werden, um ein neues Zeichen zu erzeugen. Kombinationszeichen ohne horizontalen Vorschub nehmen bei der Darstellung keinen über die Breite des Basiszeichens hinausgehenden Platz ein.

IgnoreSymbols 4

Gibt an, dass der Zeichenfolgenvergleich Symbole ignorieren muss, z.B. Leerräume, Satzzeichen, Währungssymbole, das Prozentzeichen, mathematische Symbole, das kaufmännische Und usw.

IgnoreWidth 16

Gibt an, dass beim Zeichenfolgenvergleich die Zeichenbreite ignoriert wird. Japanische Katakana-Zeichen können z. B. in voller oder halber Breite geschrieben werden. Wenn dieser Wert ausgewählt ist, werden die in voller Breite geschriebenen Katakana-Zeichen als denselben in halber Breite geschriebenen Zeichen gleichwertig betrachtet.

None 0

Gibt die Standardeinstellungen der Optionen für Zeichenfolgenvergleiche an.

Ordinal 1073741824

Gibt an, dass der Zeichenfolgenvergleich aufeinanderfolgende Unicode UTF-16-codierte Werte der Zeichenfolge verwenden muss (Vergleich von Codeeinheiten). Dies führt zu einem schnellen, jedoch kulturunabhängigen Vergleich. Eine Zeichenfolge, die mit der Codeeinheit „XXXX16“ beginnt, kommt vor einer Zeichenfolge, die mit „YYYY16“ beginnt, wenn XXXX16 kleiner als YYYY16 ist. Dieser Wert kann nicht mit anderen CompareOptions-Werten kombiniert werden und muss allein verwendet werden.

OrdinalIgnoreCase 268435456

Beim Zeichenfolgenvergleich darf die Groß- und Kleinschreibung nicht berücksichtigt werden, und anschließend muss ein ordinaler Vergleich erfolgen. Dieses Verfahren entspricht dem Konvertieren der Zeichenfolge in Großbuchstaben mithilfe der invarianten Kultur und dem anschließenden Ordinalvergleich mit dem Ergebnis.

StringSort 536870912

Gibt an, dass beim Zeichenfolgenvergleich der Zeichenfolgensortieralgorithmus verwendet werden muss. Bei der Zeichenfolgensortierung werden Bindestriche und Apostrophe sowie andere nicht alphanumerische Symbole vor alphanumerischen Zeichen aufgeführt.

Beispiele

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.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
      
      ' 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


   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

End Class


'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

Hinweise

Weitere Informationen zu dieser API finden Sie unter Zusätzliche API-Hinweise für CompareOptions.

Gilt für:

Weitere Informationen