語言

String.IComparable.CompareTo(Object) 方法

定義

比較此實例與指定的 Object 實例,並表示該實例是否在排序順序中與指定 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的物件。

傳回

一個 32 位元有符號的整數,指示此實例是否在排序順序中與參數 value 相同、跟隨或出現的位置。

價值觀狀況
低於零此實例發生 value於 之前。
零此實例在排序 value順序中與 位置相同。
大於零此實例跟隨 value,或 value 為 null。

實作

例外狀況

value 不是 String。

範例

以下範例使用 CompareTo 帶有 Object的 方法 。 由於它嘗試將實 String 例與 TestClass 物件比較,該方法會拋出一個 ArgumentException。

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
open System

type TestClass() = class end

let test = TestClass()
let objectsToCompare: obj list =
    [ test; string test; 123
      string 123; "some text"
      "Some Text" ]
let s = "some text"
for objectToCompare in objectsToCompare do
    try
        let i = s.CompareTo objectToCompare
        printfn $"Comparing '{s}' with '{objectToCompare}': {i}"
    with :? ArgumentException ->
        printfn $"Bad argument: {objectToCompare} (type {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 物體。

注意事項

此 CompareTo 方法主要設計用於排序或字母排序操作。 當方法呼叫的主要目的是判斷兩個字串是否等價時,不應使用此方法。 要判斷兩個字串是否等價,請呼叫 該 Equals 方法。

此方法利用當前文化進行詞彙(大小寫區分與文化敏感)比較。 關於字、字串和序數排序的更多資訊,請參見 System.Globalization.CompareOptions。

欲了解更多關於此方法行為的資訊,請參閱該方法的 String.Compare(String, String) 備註章節。

適用於