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);
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo

参数

value
Object

一个对象,其计算结果为 StringAn object that evaluates to a String.

返回

一个 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 之后。This instance follows value.

-or-

valuenullvalue is null.

实现

异常

value 不是 Stringvalue is not a String.

示例

下面的示例将 CompareTo 方法与 Object结合使用。The 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.

此方法使用当前区域性执行 word (区分大小写和区分区域性)比较。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.

适用于