String.IComparable.CompareTo(Object) 方法

定義

比較這個執行個體與指定的 Object,並且指出這個執行個體在排序次序中,位於所指定 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);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo

參數

value
Object

評估為 String 的物件。An object that evaluates to a String.

傳回

Int32

32 位元帶正負號的整數,指出這個執行個體在排序次序中,位於 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.

Value條件Condition
小於零Less than zero這個執行個體位於 value 之前。This instance precedes value.
Zero這個執行個體在排序次序中的位置與 value 相同。This instance has the same position in the sort order as value.
大於零Greater than zero這個執行個體位於 value 之後,或 valuenullThis instance follows value, or value is null.

實作

例外狀況

value 不是 Stringvalue is not a String.

範例

下列範例會使用 CompareTo 方法搭配 ObjectThe following example uses the CompareTo method with an Object. 因為它會嘗試比較 String 實例與物件,所以方法會擲回 TestClass ArgumentExceptionBecause 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

備註

value 必須是 String 物件。value must be a String object.

警告

CompareTo方法主要是用來排序或 Alphabetizing 作業。The CompareTo method was designed primarily for use in sorting or alphabetizing operations. 當方法呼叫的主要用途是要判斷兩個字串是否相等時,不應使用它。It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. 若要判斷兩個字串是否相等,請呼叫 Equals 方法。To determine whether two strings are equivalent, call the Equals method.

這個方法會使用目前的文化特性,執行文字 (區分大小寫且區分文化特性的) 比較。This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. 如需 word、字串和序數排序的詳細資訊,請參閱 System.Globalization.CompareOptionsFor more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

如需此方法行為的詳細資訊,請參閱方法的「備註」一節 String.Compare(String, String)For more information about the behavior of this method, see the Remarks section of the String.Compare(String, String) method.

適用於