String.IComparable.CompareTo(Object) Metoda

Definice

Porovná tuto instanci se zadaným Object a určuje, zda tato instance předchází, následuje nebo se zobrazí ve stejné pozici v pořadí řazení jako zadaný Object.Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.

 virtual int System.IComparable.CompareTo(System::Object ^ value) = IComparable::CompareTo;
int IComparable.CompareTo (object value);
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo

Parametry

value
Object

Objekt, který je vyhodnocen jako String.An object that evaluates to a String.

Návraty

32 celé číslo se znaménkem, které označuje, zda tato instance předchází, následuje nebo se zobrazuje ve stejné pozici v pořadí řazení jako parametr value.A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter.

      <table><thead><tr><th><span data-ttu-id="1d354-3512">Hodnota</span><span class="sxs-lookup"><span data-stu-id="1d354-3512">Value</span></span></th><th><span data-ttu-id="1d354-3513">Podmínka</span><span class="sxs-lookup"><span data-stu-id="1d354-3513">Condition</span></span></th></tr></thead><tbody><tr><td><span data-ttu-id="1d354-3514">Menší než nula</span><span class="sxs-lookup"><span data-stu-id="1d354-3514">Less than zero</span></span></td><td><span data-ttu-id="1d354-3515">Tato instance předchází <code data-dev-comment-type="paramref">value</code>.</span><span class="sxs-lookup"><span data-stu-id="1d354-3515">This instance precedes <code data-dev-comment-type="paramref">value</code>.</span></span></td></tr><tr><td><span data-ttu-id="1d354-3516">Nula</span><span class="sxs-lookup"><span data-stu-id="1d354-3516">Zero</span></span></td><td><span data-ttu-id="1d354-3517">Tato instance má stejné umístění v pořadí řazení jako <code data-dev-comment-type="paramref">value</code>.</span><span class="sxs-lookup"><span data-stu-id="1d354-3517">This instance has the same position in the sort order as <code data-dev-comment-type="paramref">value</code>.</span></span></td></tr><tr><td><span data-ttu-id="1d354-3518">Větší než nula</span><span class="sxs-lookup"><span data-stu-id="1d354-3518">Greater than zero</span></span></td><td><span data-ttu-id="1d354-3519">Tato instance následuje <code data-dev-comment-type="paramref">value</code>.</span><span class="sxs-lookup"><span data-stu-id="1d354-3519">This instance follows <code data-dev-comment-type="paramref">value</code>.</span></span>

-nebo--or-

            <span data-ttu-id="1d354-3521"><code data-dev-comment-type="paramref">value</code> je <code data-dev-comment-type="langword">null</code>.</span><span class="sxs-lookup"><span data-stu-id="1d354-3521"><code data-dev-comment-type="paramref">value</code> is <code data-dev-comment-type="langword">null</code>.</span></span></td></tr></tbody></table>

Implementuje

Výjimky

value není String.value is not a String.

Příklady

Následující příklad používá metodu CompareTo s Object.The following example uses the CompareTo method with an Object. Vzhledem k tomu, že se pokusí porovnat instanci String s objektem TestClass, vyvolá metoda ArgumentException.Because it attempts to compare a String instance to a TestClass object, the method throws an ArgumentException.

using namespace System;

public ref class TestClass{};

int main()
{
   TestClass^ test = gcnew TestClass;
   array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
                                        (123).ToString(), "some text",
                                        "Some Text" };
   String^ s = "some text";
   for each (Object^ objectToCompare in objectsToCompare) {
      try {
         Int32 i = s->CompareTo(objectToCompare);
         Console::WriteLine("Comparing '{0}' with '{1}': {2}",
                            s, objectToCompare, i);
      }
      catch (ArgumentException^ e) {
         Console::WriteLine("Bad argument: {0} (type {1})",
                            objectToCompare,
                            objectToCompare->GetType()->Name);
      }
   }
}
// The example displays the following output:
//    Bad argument: TestClass (type TestClass)
//    Comparing 'some text' with 'TestClass': -1
//    Bad argument: 123 (type Int32)
//    Comparing 'some text' with '123': 1
//    Comparing 'some text' with 'some text': 0
//    Comparing 'some text' with 'Some Text': -1
using System;

public class TestClass
{}

public class Example 
{
   public static void Main()
   {
      var test = new TestClass();
      Object[] objectsToCompare = { test, test.ToString(), 123,
                                    123.ToString(), "some text",
                                    "Some Text" };
      string s = "some text";
      foreach (var objectToCompare in objectsToCompare) {
         try {
            int i = s.CompareTo(objectToCompare);
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i);
         }
         catch (ArgumentException) {
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name);
         }
      }
   }
}
// The example displays the following output:
//    Bad argument: TestClass (type TestClass)
//    Comparing 'some text' with 'TestClass': -1
//    Bad argument: 123 (type Int32)
//    Comparing 'some text' with '123': 1
//    Comparing 'some text' with 'some text': 0
//    Comparing 'some text' with 'Some Text': -1
Public Class TestClass
End Class 


Public Class Example
   Public Shared Sub Main()
      Dim test As New TestClass()
      Dim objectsToCompare() As Object = { test, test.ToString(), 123,
                                           123.ToString(), "some text",
                                           "Some Text" }
      Dim s As String = "some text"
      For Each objectToCompare In objectsToCompare
         Try
            Dim i As Integer = s.CompareTo(objectToCompare)
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i)
         Catch e As ArgumentException
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name)
         End Try
      Next
   End Sub 
End Class 
' The example displays the following output:
'       Bad argument: TestClass (type TestClass)
'       Comparing 'some text' with 'TestClass': -1
'       Bad argument: 123 (type Int32)
'       Comparing 'some text' with '123': 1
'       Comparing 'some text' with 'some text': 0
'       Comparing 'some text' with 'Some Text': -1

Poznámky

value musí být objekt String.value must be a String object.

Upozornění

Metoda CompareTo byla navržena hlavně pro použití při řazení nebo alphabetizing operací.The CompareTo method was designed primarily for use in sorting or alphabetizing operations. Neměl by být použit, pokud je primárním účelem volání metody určit, zda jsou dva řetězce ekvivalentní.It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. Chcete-li zjistit, zda jsou dva řetězce ekvivalentní, zavolejte metodu Equals.To determine whether two strings are equivalent, call the Equals method.

Tato metoda provádí porovnání slov (velká a malá písmena a specifika jazykové verze) použitím aktuální jazykové verze.This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. Další informace o aplikaci Word, řetězci a pořadí řazení naleznete v tématu System.Globalization.CompareOptions.For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

Další informace o chování této metody naleznete v části poznámky v metodě String.Compare(String, String).For more information about the behavior of this method, see the Remarks section of the String.Compare(String, String) method.

Platí pro