String.CompareOrdinal 메서드

정의

각 문자열에서 해당하는 String 개체의 숫자 값을 계산해서 두 Char 개체를 비교합니다.

오버로드

CompareOrdinal(String, String)

각 문자열에서 해당하는 String 개체의 숫자 값을 계산해서 두 지정된 Char 개체를 비교합니다.

CompareOrdinal(String, Int32, String, Int32, Int32)

각 부분 문자열에서 해당하는 String 개체의 숫자 값을 계산하여 지정된 두 Char 개체의 부분 문자열을 비교합니다.

CompareOrdinal(String, String)

Source:
String.Comparison.cs
Source:
String.Comparison.cs
Source:
String.Comparison.cs

각 문자열에서 해당하는 String 개체의 숫자 값을 계산해서 두 지정된 Char 개체를 비교합니다.

public:
 static int CompareOrdinal(System::String ^ strA, System::String ^ strB);
public static int CompareOrdinal (string strA, string strB);
public static int CompareOrdinal (string? strA, string? strB);
static member CompareOrdinal : string * string -> int
Public Shared Function CompareOrdinal (strA As String, strB As String) As Integer

매개 변수

strA
String

비교할 첫째 문자열입니다.

strB
String

비교할 둘째 문자열입니다.

반환

두 비교 대상 간의 어휘 관계를 나타내는 정수입니다.

조건
0보다 작음strAstrB보다 작은 경우
0strAstrB가 같은 경우
0보다 큼strAstrB보다 큰 경우

예제

다음 예제에서는 대/소문자만 다른 두 문자열의 서수 비교를 수행합니다.

// Sample for String::CompareOrdinal(String, String)
using namespace System;
int main()
{
   String^ str1 = "ABCD";
   String^ str2 = "abcd";
   String^ str;
   int result;
   Console::WriteLine();
   Console::WriteLine( "Compare the numeric values of the corresponding Char objects in each string." );
   Console::WriteLine( "str1 = '{0}', str2 = '{1}'", str1, str2 );
   result = String::CompareOrdinal( str1, str2 );
   str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to"));
   Console::Write( "String '{0}' is ", str1 );
   Console::Write( "{0} ", str );
   Console::WriteLine( "String '{0}'.", str2 );
}

/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/
// Sample for String.CompareOrdinal(String, String)
using System;

class Sample {
    public static void Main() {
    String str1 = "ABCD";
    String str2 = "abcd";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/
// Sample for String.CompareOrdinal(String, String)
open System

let str1 = "ABCD"
let str2 = "abcd"

printfn "\nCompare the numeric values of the corresponding Char objects in each string."
printfn $"str1 = '{str1}', str2 = '{str2}'"
let result = String.CompareOrdinal(str1, str2)
let str = if result < 0 then "less than" elif result > 0 then "greater than" else "equal to"
printf $"String '{str1}' is "
printf $"{str} "
printfn $"String '{str2}'."

(*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*)
' Sample for String.CompareOrdinal(String, String)
Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer
      
      Console.WriteLine()
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.")
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
      result = [String].CompareOrdinal(str1, str2)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("String '{0}' is ", str1)
      Console.Write("{0} ", str)
      Console.WriteLine("String '{0}'.", str2)
   End Sub
End Class
'
'This example produces the following results:
'
'Compare the numeric values of the corresponding Char objects in each string.
'str1 = 'ABCD', str2 = 'abcd'
'String 'ABCD' is less than String 'abcd'.
'

설명

이 메서드는 서수 정렬 규칙을 사용하여 대/소문자를 구분하는 비교를 수행합니다. 단어, 문자열 및 서수 정렬에 대한 자세한 내용은 를 참조하세요 System.Globalization.CompareOptions. 서수 정렬 규칙을 사용하여 대/소문자를 구분하지 않는 비교를 수행하려면 인수가 Compare(String, String, StringComparison) 로 설정된 메서드를 comparisonType 호출합니다 StringComparison.OrdinalIgnoreCase.

가 정적 메서드이고 strBstrA 일 수 있기 때문 CompareOrdinal(String, String)null입니다. 두 값이 모두 이면 메서드는 nullstrB 가 같음 strA 을 나타내는 0을 반환합니다. 값 중 하나만 이 null면 메서드는 null이 아닌 값을 더 큰 값으로 간주합니다.

추가 정보

적용 대상

CompareOrdinal(String, Int32, String, Int32, Int32)

Source:
String.Comparison.cs
Source:
String.Comparison.cs
Source:
String.Comparison.cs

각 부분 문자열에서 해당하는 String 개체의 숫자 값을 계산하여 지정된 두 Char 개체의 부분 문자열을 비교합니다.

public:
 static int CompareOrdinal(System::String ^ strA, int indexA, System::String ^ strB, int indexB, int length);
public static int CompareOrdinal (string strA, int indexA, string strB, int indexB, int length);
public static int CompareOrdinal (string? strA, int indexA, string? strB, int indexB, int length);
static member CompareOrdinal : string * int * string * int * int -> int
Public Shared Function CompareOrdinal (strA As String, indexA As Integer, strB As String, indexB As Integer, length As Integer) As Integer

매개 변수

strA
String

비교에 사용할 첫 번째 문자열입니다.

indexA
Int32

strA에 있는 부분 문자열의 시작 인덱스입니다.

strB
String

비교에 사용할 두 번째 문자열입니다.

indexB
Int32

strB에 있는 부분 문자열의 시작 인덱스입니다.

length
Int32

비교할 부분 문자열의 최대 문자 수입니다.

반환

두 비교 대상 간의 어휘 관계를 나타내는 32비트 부호 있는 정수입니다.

조건
0보다 작음 strA의 부분 문자열이 strB의 부분 문자열보다 작습니다.
0 부분 문자열이 같거나 length가 0입니다.
0보다 큼 strA의 부분 문자열이 strB의 부분 문자열보다 작습니다.

예외

strAnull 이 아니고, indexAstrA.Length보다 큽니다.

또는

strBnull 이 아니고, indexBstrB.Length보다 큽니다.

또는

indexA, indexB 또는 length가 음수입니다.

예제

다음 예제에서는 및 Compare 가 다른 정렬 순서를 사용하는 것을 CompareOrdinal 보여 줍니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   String^ strLow = "abc";
   String^ strCap = "ABC";
   String^ result = "equal to ";
   int x = 0;
   int pos = 1;
   
   // The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
   x = String::CompareOrdinal( strLow, pos, strCap, pos, 1 );
   if ( x < 0 )
      result = "less than";

   if ( x > 0 )
      result = "greater than";

   Console::WriteLine( "CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos );
   Console::WriteLine( "   '{0}' is {1} '{2}'", strLow[ pos ], result, strCap[ pos ] );

   // In U.S. English culture, 'b' is linguistically less than 'B'.
   x = String::Compare( strLow, pos, strCap, pos, 1, false, gcnew CultureInfo( "en-US" ) );
   if ( x < 0 )
      result = "less than";
   else
   if ( x > 0 )
      result = "greater than";

   Console::WriteLine( "Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos );
   Console::WriteLine( "   '{0}' is {1} '{2}'", strLow[ pos ], result, strCap[ pos ] );
}
using System;
using System.Globalization;

class Test
{
    public static void Main(String[] args)
    {
    String strLow = "abc";
    String strCap = "ABC";
    String result = "equal to ";
    int x = 0;
    int pos = 1;

// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
    x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
    if (x < 0) result = "less than";
    if (x > 0) result = "greater than";
    Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

// In U.S. English culture, 'b' is linguistically less than 'B'.
    x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
    if (x < 0) result = "less than";
    else if (x > 0) result = "greater than";
    Console.WriteLine("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
    }
}
open System
open System.Globalization

[<EntryPoint>]
let main _ =
    let strLow = "abc"
    let strCap = "ABC"
    let result = "equal to "
    let pos = 1

    // The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
    let x = String.CompareOrdinal(strLow, pos, strCap, pos, 1)
    let result =
        if x < 0 then "less than"
        elif x > 0 then "greater than"
        else "equal to"
    printfn $"CompareOrdinal(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
    printfn $"   '{strLow[pos]}' is {result} '{strCap[pos]}'"

    // In U.S. English culture, 'b' is linguistically less than 'B'.
    let x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"))
    let result =
        if x < 0 then "less than"
        elif x > 0 then "greater than"
        else "equal to"
    printfn $"Compare(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
    printfn $"   '{strLow[pos]}' is {result} '{strCap[pos]}'"
    0
Imports System.Globalization

Class Test
   
  Public Shared Sub Main(args() As [String])
      Dim strLow As [String] = "abc"
      Dim strCap As [String] = "ABC"
      Dim result As [String] = "equal to "
      Dim x As Integer = 0
      Dim pos As Integer = 1

' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.      
      x = [String].CompareOrdinal(strLow, pos, strCap, pos, 1)
      If x < 0 Then
         result = "less than"
      End If
      If x > 0 Then
         result = "greater than"
      End If

' In U.S. English culture, 'b' is linguistically less than 'B'.
      Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
      
      x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
   End Sub
End Class

설명

, indexBlength 매개 변수는 indexAnnnegative여야 합니다.

비교된 문자 수는 길이 strAindexA가 더 작을수록 이고, 길이 strB 는 , 및 length보다 작indexB습니다.

이 메서드는 서수 정렬 규칙을 사용하여 대/소문자를 구분하는 비교를 수행합니다. 단어, 문자열 및 서수 정렬에 대한 자세한 내용은 를 참조하세요 System.Globalization.CompareOptions. 서수 정렬 규칙을 사용하여 대/소문자를 구분하지 않는 비교를 수행하려면 인수가 Compare(String, Int32, String, Int32, Int32, StringComparison) 로 설정된 메서드를 comparisonType 호출합니다 StringComparison.OrdinalIgnoreCase.

가 정적 메서드이고 strBstrA 일 수 있기 때문 CompareOrdinal(String, String)null입니다. 두 값이 모두 이면 메서드는 nullstrB 가 같음 strA 을 나타내는 0을 반환합니다. 값 중 하나만 이 null면 메서드는 null이 아닌 값을 더 큰 값으로 간주합니다.

추가 정보

적용 대상