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이 되는 개체입니다.

반환

정렬 순서에서 이 인스턴스의 위치가 value 매개 변수보다 앞인지, 뒤인지 또는 동일한지를 나타내는 32비트 부호 있는 정수입니다.

조건
0보다 작음이 인스턴스가 value 앞에 오는 경우
0이 인스턴스의 위치가 정렬 순서에서 value와 같은 경우
0보다 큼이 인스턴스가 value 다음에 오거나 valuenull인 경우

구현

예외

value이(가) String가 아닌 경우

예제

다음 예제에서는 와 함께 메서드를 Object사용합니다CompareTo. 인스턴스 TestClass 를 개체와 비교 String 하려고 하므로 메서드는 을 ArgumentExceptionthrow합니다.

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
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) 참조하세요.

적용 대상