CompareInfo.Compare Methode

Definition

Vergleicht zwei Zeichenfolgen.

Überlädt

Compare(String, String)

Vergleicht zwei Zeichenfolgen.

Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

Vergleicht zwei schreibgeschützte Zeichenspannen.

Compare(String, String, CompareOptions)

Vergleicht mithilfe des angegebenen CompareOptions-Werts zwei Zeichenfolgen.

Compare(String, Int32, String, Int32)

Vergleicht den Endabschnitt einer Zeichenfolge mit dem Endabschnitt einer anderen Zeichenfolge.

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

Vergleicht mithilfe des angegebenen CompareOptions-Werts den Endabschnitt einer Zeichenfolge mit dem Endabschnitt einer anderen Zeichenfolge.

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

Vergleicht einen Abschnitt einer Zeichenfolge mit einem Abschnitt einer anderen Zeichenfolge.

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

Vergleicht mithilfe des angegebenen CompareOptions-Werts einen Abschnitt einer Zeichenfolge mit einem Abschnitt einer anderen Zeichenfolge.

Compare(String, String)

Vergleicht zwei Zeichenfolgen.

public:
 virtual int Compare(System::String ^ string1, System::String ^ string2);
public:
 int Compare(System::String ^ string1, System::String ^ string2);
public virtual int Compare (string string1, string string2);
public int Compare (string? string1, string? string2);
public virtual int Compare (string? string1, string? string2);
abstract member Compare : string * string -> int
override this.Compare : string * string -> int
member this.Compare : string * string -> int
Public Overridable Function Compare (string1 As String, string2 As String) As Integer
Public Function Compare (string1 As String, string2 As String) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

string2
String

Die zweite zu vergleichende Zeichenfolge.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null)string1 ist kleiner als string2.
größer als 0 (Null)string1 ist größer als string2.

Beispiele

Im folgenden Beispiel werden Teile von zwei Zeichenfolgen mit den verschiedenen CompareInfo -Objekten verglichen:

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit internationaler Sortierung zugeordnet ist

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit traditioneller Sortierung zugeordnet ist

  • CompareInfo -Objekt, das dem zugeordnet ist InvariantCulture

// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the S"Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the S"Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.
using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
   Console::WriteLine( "   With myCompIntl::Compare: {0}", myCompIntl->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With myCompTrad::Compare: {0}", myCompTrad->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With myCompInva::Compare: {0}", myCompInva->Compare( myStr1, myStr2 ) );
}

/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl::Compare: -1
   With myCompTrad::Compare: 1
   With myCompInva::Compare: -1
*/
// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );
   }
}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
' The following code example compares two strings using the different CompareInfo instances:
'    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
'    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
'    a CompareInfo instance associated with the InvariantCulture.

Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, myStr2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "calle" and "calor"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

Im folgenden Beispiel wird das Aufrufen der Compare-Methode veranschaulicht.

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

Hinweise

Standardmäßig wird der Vergleich mithilfe CompareOptions.Nonevon ausgeführt. Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden aufrufen, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen, d. h. Zeichen, die bei der Durchführung eines linguistischen oder kulturabhängigen Vergleichs nicht berücksichtigt werden. Die Compare(String, String) -Methode berücksichtigt solche Zeichen nicht, wenn sie einen kulturabhängigen Vergleich durchführt. Beispielsweise gibt ein kulturabhängiger Vergleich von "animal" mit "ani-mal" (mit einem weichen Bindestrich oder U+00AD) an, dass die beiden Zeichenfolgen gleichwertig sind, wie im folgenden Beispiel gezeigt.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
      Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
      Dim s2 As String = "animal"
      
      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, s2, ci.Compare(s1, s2))
  End Sub
End Module
' The example displays the following output:
'       Comparison of 'ani-mal' and 'animal': 0

Um ignorierbare Zeichen in einem Zeichenfolgenvergleich zu erkennen, rufen Sie die Compare(String, String, CompareOptions) -Methode auf, und geben Sie den Wert oder OrdinalOrdinalIgnoreCase für den options -Parameter an.

Gilt für:

Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

Vergleicht zwei schreibgeschützte Zeichenspannen.

public int Compare (ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.Compare : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions -> int
Public Function Compare (string1 As ReadOnlySpan(Of Char), string2 As ReadOnlySpan(Of Char), Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Integer

Parameter

string1
ReadOnlySpan<Char>

Die erste schreibgeschützte Zeichenspanne, die verglichen werden soll.

string2
ReadOnlySpan<Char>

Die zweite schreibgeschützte Zeichenspanne, die verglichen werden soll.

options
CompareOptions

Eine optionale Kombination aus CompareOptions-Enumerationswerten, die während des Vergleichs verwendet werden sollen. Der Standardwert ist None.

Gibt zurück

0, wenn string1 und string2 identisch sind, oder ein negativer Wert, wenn string1 vor string2 sortiert wird, oder ein positiver Wert, wenn string1 vor string2 sortiert wird.

Ausnahmen

options enthält eine nicht unterstützte Flagkombination.

Gilt für:

Compare(String, String, CompareOptions)

Vergleicht mithilfe des angegebenen CompareOptions-Werts zwei Zeichenfolgen.

public:
 virtual int Compare(System::String ^ string1, System::String ^ string2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, System::String ^ string2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, string string2, System.Globalization.CompareOptions options);
public int Compare (string? string1, string? string2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, string? string2, System.Globalization.CompareOptions options);
abstract member Compare : string * string * System.Globalization.CompareOptions -> int
override this.Compare : string * string * System.Globalization.CompareOptions -> int
member this.Compare : string * string * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, string2 As String, options As CompareOptions) As Integer
Public Function Compare (string1 As String, string2 As String, options As CompareOptions) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

string2
String

Die zweite zu vergleichende Zeichenfolge.

options
CompareOptions

Ein Wert, der definiert, wie string1 und string2 verglichen werden sollen. options ist entweder der allein verwendete Enumerationswert Ordinal oder a bitweise Kombination eines oder mehrerer der folgenden Werte: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth , IgnoreKanaType und StringSort.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null)string1 ist kleiner als string2.
größer als 0 (Null)string1 ist größer als string2.

Ausnahmen

options enthält einen ungültigen CompareOptions-Wert.

Beispiele

Im folgenden Beispiel werden zwei Zeichenfolgen mit unterschiedlichen CompareOptions Einstellungen verglichen.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo which uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, myStr2, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "My Uncle Bill's clients" and "My uncle bills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "My Uncle Bill's clients" and "My uncle bills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, myStr2))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "My Uncle Bill's clients" and "My uncle bills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0

Im folgenden Beispiel wird das Aufrufen der Compare-Methode veranschaulicht.

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

Hinweise

Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden aufrufen, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen, d. h. Zeichen, die bei der Durchführung eines linguistischen oder kulturabhängigen Vergleichs nicht berücksichtigt werden. Die Compare(String, String, CompareOptions) -Methode berücksichtigt solche Zeichen nicht, wenn sie einen kulturabhängigen Vergleich durchführt. Um ignorierbare Zeichen im Vergleich zu erkennen, geben Sie den Wert Ordinal oder OrdinalIgnoreCase für den options Parameter an.

Weitere Informationen

Gilt für:

Compare(String, Int32, String, Int32)

Vergleicht den Endabschnitt einer Zeichenfolge mit dem Endabschnitt einer anderen Zeichenfolge.

public:
 virtual int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2);
public:
 int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2);
public virtual int Compare (string string1, int offset1, string string2, int offset2);
public int Compare (string? string1, int offset1, string? string2, int offset2);
public virtual int Compare (string? string1, int offset1, string? string2, int offset2);
abstract member Compare : string * int * string * int -> int
override this.Compare : string * int * string * int -> int
member this.Compare : string * int * string * int -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer) As Integer
Public Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

offset1
Int32

Der nullbasierte Index des Zeichens in string1, an dem der Vergleich beginnen soll.

string2
String

Die zweite zu vergleichende Zeichenfolge.

offset2
Int32

Der nullbasierte Index des Zeichens in string2, an dem der Vergleich beginnen soll.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null) Der angegebene Abschnitt von string1 ist kleiner als der angegebene Abschnitt von string2.
größer als 0 (Null) Der angegebene Abschnitt von string1 ist größer als der angegebene Abschnitt von string2.

Ausnahmen

offset1 oder offset2 ist kleiner als 0.

- oder -

offset1 ist größer oder gleich der Anzahl von Zeichen in string1.

- oder -

offset2 ist größer oder gleich der Anzahl von Zeichen in string2.

Beispiele

Im folgenden Beispiel werden Teile von zwei Zeichenfolgen mit den verschiedenen CompareInfo -Objekten verglichen:

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit internationaler Sortierung zugeordnet ist

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit traditioneller Sortierung zugeordnet ist

  • CompareInfo -Objekt, das dem zugeordnet ist InvariantCulture

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 2 ), myStr2->Substring( 2 ) );
   Console::WriteLine( "   With myCompIntl::Compare: {0}", myCompIntl->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompTrad::Compare: {0}", myCompTrad->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompInva::Compare: {0}", myCompInva->Compare( myStr1, 2, myStr2, 2 ) );
}

/*
This code produces the following output.

Comparing "lle" and "lor"
   With myCompIntl::Compare: -1
   With myCompTrad::Compare: 1
   With myCompInva::Compare: -1
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 2 ), myStr2.Substring( 2 ) );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, 2, myStr2, 2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, 2, myStr2, 2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, 2, myStr2, 2 ) );
   }
}


/*
This code produces the following output.

Comparing "lle" and "lor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(2), myStr2.Substring(2))
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, 2, myStr2, 2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, 2, myStr2, 2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, 2, myStr2, 2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "lle" and "lor"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

Hinweise

Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden aufrufen, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen. Die Compare(String, Int32, String, Int32) Methode berücksichtigt diese Zeichen nicht, wenn sie einen linguistischen oder kulturabhängigen Vergleich durchführt. Um ignorierbare Zeichen im Vergleich zu erkennen, rufen Sie die Compare(String, Int32, String, Int32, CompareOptions) -Methode auf, und geben Sie den Wert oder OrdinalOrdinalIgnoreCase für den options Parameter an.

Gilt für:

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

Vergleicht mithilfe des angegebenen CompareOptions-Werts den Endabschnitt einer Zeichenfolge mit dem Endabschnitt einer anderen Zeichenfolge.

public:
 virtual int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, int offset1, string string2, int offset2, System.Globalization.CompareOptions options);
public int Compare (string? string1, int offset1, string? string2, int offset2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, int offset1, string? string2, int offset2, System.Globalization.CompareOptions options);
abstract member Compare : string * int * string * int * System.Globalization.CompareOptions -> int
override this.Compare : string * int * string * int * System.Globalization.CompareOptions -> int
member this.Compare : string * int * string * int * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer, options As CompareOptions) As Integer
Public Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer, options As CompareOptions) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

offset1
Int32

Der nullbasierte Index des Zeichens in string1, an dem der Vergleich beginnen soll.

string2
String

Die zweite zu vergleichende Zeichenfolge.

offset2
Int32

Der nullbasierte Index des Zeichens in string2, an dem der Vergleich beginnen soll.

options
CompareOptions

Ein Wert, der definiert, wie string1 und string2 verglichen werden sollen. options ist entweder der allein verwendete Enumerationswert Ordinal oder a bitweise Kombination eines oder mehrerer der folgenden Werte: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth , IgnoreKanaType und StringSort.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null) Der angegebene Abschnitt von string1 ist kleiner als der angegebene Abschnitt von string2.
größer als 0 (Null) Der angegebene Abschnitt von string1 ist größer als der angegebene Abschnitt von string2.

Ausnahmen

offset1 oder offset2 ist kleiner als 0.

- oder -

offset1 ist größer oder gleich der Anzahl von Zeichen in string1.

- oder -

offset2 ist größer oder gleich der Anzahl von Zeichen in string2.

options enthält einen ungültigen CompareOptions-Wert.

Beispiele

Im folgenden Beispiel werden Teile von zwei Zeichenfolgen mit unterschiedlichen CompareOptions Einstellungen verglichen.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 10 ), myStr2->Substring( 10 ) );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, 10, myStr2, 10 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, 10, myStr2, 10, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "ill's clients" and "ills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -76
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 0
   With IgnoreCase and IgnoreSymbols : 0
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 10 ), myStr2.Substring( 10 ) );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, 10, myStr2, 10 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "ill's clients" and "ills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -76
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 0
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(10), myStr2.Substring(10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 10, myStr2, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "ill's clients" and "ills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -76
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 0
'   With IgnoreCase and IgnoreSymbols : 0

Hinweise

Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden aufrufen, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen, d. h. Zeichen, die bei der Durchführung eines linguistischen oder kulturabhängigen Vergleichs nicht berücksichtigt werden. Die Compare(String, Int32, String, Int32, CompareOptions) -Methode berücksichtigt solche Zeichen nicht, wenn sie einen kulturabhängigen Vergleich durchführt. Um ignorierbare Zeichen im Vergleich zu erkennen, geben Sie den Wert Ordinal oder OrdinalIgnoreCase für den options Parameter an.

Weitere Informationen

Gilt für:

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

Vergleicht einen Abschnitt einer Zeichenfolge mit einem Abschnitt einer anderen Zeichenfolge.

public:
 virtual int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2);
public:
 int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2);
public virtual int Compare (string string1, int offset1, int length1, string string2, int offset2, int length2);
public int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2);
public virtual int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2);
abstract member Compare : string * int * int * string * int * int -> int
override this.Compare : string * int * int * string * int * int -> int
member this.Compare : string * int * int * string * int * int -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer) As Integer
Public Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

offset1
Int32

Der nullbasierte Index des Zeichens in string1, an dem der Vergleich beginnen soll.

length1
Int32

Die Anzahl der zu vergleichenden aufeinander folgenden Zeichen in string1.

string2
String

Die zweite zu vergleichende Zeichenfolge.

offset2
Int32

Der nullbasierte Index des Zeichens in string2, an dem der Vergleich beginnen soll.

length2
Int32

Die Anzahl der zu vergleichenden aufeinander folgenden Zeichen in string2.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null) Der angegebene Abschnitt von string1 ist kleiner als der angegebene Abschnitt von string2.
größer als 0 (Null) Der angegebene Abschnitt von string1 ist größer als der angegebene Abschnitt von string2.

Ausnahmen

offset1, length1, offset2 oder length2 ist kleiner als 0 (Null).

- oder -

offset1 ist größer oder gleich der Anzahl von Zeichen in string1.

- oder -

offset2 ist größer oder gleich der Anzahl von Zeichen in string2.

- oder -

length1 ist größer als die Anzahl der Zeichen ab offset1 bis zum Ende von string1.

- oder -

length2 ist größer als die Anzahl der Zeichen ab offset2 bis zum Ende von string2.

Beispiele

Im folgenden Beispiel werden Teile von zwei Zeichenfolgen mit den verschiedenen CompareInfo -Objekten verglichen:

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit internationaler Sortierung zugeordnet ist

  • CompareInfo Objekt, das der spanischen Kultur (Spanien) mit traditioneller Sortierung zugeordnet ist

  • CompareInfo -Objekt, das dem zugeordnet ist InvariantCulture

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \" {0}\" and \" {1}\"", myStr1->Substring( 2, 2 ), myStr2->Substring( 2, 2 ) );
   Console::WriteLine( "   With myCompIntl->Compare: {0}", myCompIntl->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   Console::WriteLine( "   With myCompTrad->Compare: {0}", myCompTrad->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   Console::WriteLine( "   With myCompInva->Compare: {0}", myCompInva->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
}

/*
This code produces the following output.

Comparing S"ll" and S"lo"
With myCompIntl.Compare: -1
With myCompTrad.Compare: 1
With myCompInva.Compare: -1

*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 2, 2 ), myStr2.Substring( 2, 2 ) );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   }
}


/*
This code produces the following output.

Comparing "ll" and "lo"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(2, 2), myStr2.Substring(2, 2))
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, 2, 2, myStr2, 2, 2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "ll" and "lo"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

Hinweise

Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden verwenden, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen. Die Compare(String, Int32, Int32, String, Int32, Int32) Methode berücksichtigt diese Zeichen nicht, wenn sie einen linguistischen oder kulturabhängigen Vergleich durchführt. Um ignorierbare Zeichen im Vergleich zu erkennen, rufen Sie die Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) -Methode auf, und geben Sie den Wert oder OrdinalOrdinalIgnoreCase für den options Parameter an.

Gilt für:

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

Vergleicht mithilfe des angegebenen CompareOptions-Werts einen Abschnitt einer Zeichenfolge mit einem Abschnitt einer anderen Zeichenfolge.

public:
 virtual int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, int offset1, int length1, string string2, int offset2, int length2, System.Globalization.CompareOptions options);
public int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2, System.Globalization.CompareOptions options);
abstract member Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
override this.Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
member this.Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer, options As CompareOptions) As Integer
Public Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer, options As CompareOptions) As Integer

Parameter

string1
String

Die erste zu vergleichende Zeichenfolge.

offset1
Int32

Der nullbasierte Index des Zeichens in string1, an dem der Vergleich beginnen soll.

length1
Int32

Die Anzahl der zu vergleichenden aufeinander folgenden Zeichen in string1.

string2
String

Die zweite zu vergleichende Zeichenfolge.

offset2
Int32

Der nullbasierte Index des Zeichens in string2, an dem der Vergleich beginnen soll.

length2
Int32

Die Anzahl der zu vergleichenden aufeinander folgenden Zeichen in string2.

options
CompareOptions

Ein Wert, der definiert, wie string1 und string2 verglichen werden sollen. options ist entweder der allein verwendete Enumerationswert Ordinal oder a bitweise Kombination eines oder mehrerer der folgenden Werte: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth , IgnoreKanaType und StringSort.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die die lexikalische Beziehung der beiden verglichenen Elemente angibt.

Wert Bedingung
Null Die beiden Zeichenfolgen sind gleich.
kleiner als 0 (Null) Der angegebene Abschnitt von string1 ist kleiner als der angegebene Abschnitt von string2.
größer als 0 (Null) Der angegebene Abschnitt von string1 ist größer als der angegebene Abschnitt von string2.

Ausnahmen

offset1, length1, offset2 oder length2 ist kleiner als 0 (Null).

- oder -

offset1 ist größer oder gleich der Anzahl von Zeichen in string1.

- oder -

offset2 ist größer oder gleich der Anzahl von Zeichen in string2.

- oder -

length1 ist größer als die Anzahl der Zeichen ab offset1 bis zum Ende von string1.

- oder -

length2 ist größer als die Anzahl der Zeichen ab offset2 bis zum Ende von string2.

options enthält einen ungültigen CompareOptions-Wert.

Beispiele

Im folgenden Beispiel werden Teile von zwei Zeichenfolgen mit unterschiedlichen CompareOptions Einstellungen verglichen.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 3, 10 ), myStr2->Substring( 3, 10 ) );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 3, 10 ), myStr2.Substring( 3, 10 ) );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(3, 10), myStr2.Substring(3, 10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "Uncle Bill" and "uncle bill"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : 1
'   With IgnoreCase                   : 0
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0

Hinweise

Wenn eine Sicherheitsentscheidung von einem Zeichenfolgenvergleich oder einer Änderung der Groß-/Kleinschreibung abhängt, sollten Sie die InvariantCulture -Eigenschaft verwenden, um sicherzustellen, dass das Verhalten unabhängig von den Kultureinstellungen des Betriebssystems konsistent ist.

Hinweis

Wenn möglich, sollten Sie Zeichenfolgenvergleichsmethoden aufrufen, die über einen Parameter vom Typ CompareOptions verfügen, um die art des erwarteten Vergleichs anzugeben. Verwenden Sie in der Regel linguistische Optionen (unter Verwendung der aktuellen Kultur), um Zeichenfolgen zu vergleichen, die auf der Benutzeroberfläche angezeigt werden, und geben Sie Ordinal oder OrdinalIgnoreCase für Sicherheitsvergleiche an.

Hinweise für Aufrufer

Zeichensätze enthalten ignorierbare Zeichen. Die Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) -Methode berücksichtigt diese Zeichen nicht, wenn sie einen kulturabhängigen Vergleich durchführt. Um ignorierbare Zeichen im Vergleich zu erkennen, geben Sie den Wert Ordinal oder OrdinalIgnoreCase für den options Parameter an.

Weitere Informationen

Gilt für: