CompareInfo.Compare 메서드

정의

두 문자열을 비교합니다.

오버로드

Compare(String, String)

두 문자열을 비교합니다.

Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

두 개의 읽기 전용 문자 범위를 비교합니다.

Compare(String, String, CompareOptions)

지정된 CompareOptions 값을 사용하여 두 문자열을 비교합니다.

Compare(String, Int32, String, Int32)

한 문자열의 끝 영역을 다른 문자열의 끝 영역과 비교합니다.

Compare(String, Int32, String, Int32, CompareOptions)

지정된 CompareOptions 값을 사용하여 한 문자열의 끝 영역을 다른 문자열의 시작 영역과 비교합니다.

Compare(String, Int32, Int32, String, Int32, Int32)

한 문자열의 특정 영역을 다른 문자열의 특정 영역과 비교합니다.

Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions)

지정된 CompareOptions 값을 사용하여 한 문자열의 특정 영역을 다른 문자열의 특정 영역과 비교합니다.

Compare(String, String)

두 문자열을 비교합니다.

public:
 virtual int Compare(System::String ^ string1, System::String ^ string2);
public:
 int Compare(System::String ^ string1, System::String ^ string2);
public virtual int Compare (string string1, string string2);
public int Compare (string? string1, string? string2);
public virtual int Compare (string? string1, string? string2);
abstract member Compare : string * string -> int
override this.Compare : string * string -> int
member this.Compare : string * string -> int
Public Overridable Function Compare (string1 As String, string2 As String) As Integer
Public Function Compare (string1 As String, string2 As String) As Integer

매개 변수

string1
String

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

string2
String

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

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 string1string2보다 작은 경우
0보다 큼 string1string2보다 큰 경우

예제

다음 예제에서는 서로 다른 CompareInfo 개체를 사용하여 두 문자열의 일부를 비교합니다.

// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the S"Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the S"Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.
using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
   Console::WriteLine( "   With myCompIntl::Compare: {0}", myCompIntl->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With myCompTrad::Compare: {0}", myCompTrad->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With myCompInva::Compare: {0}", myCompInva->Compare( myStr1, myStr2 ) );
}

/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl::Compare: -1
   With myCompTrad::Compare: 1
   With myCompInva::Compare: -1
*/
// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );
   }
}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
' The following code example compares two strings using the different CompareInfo instances:
'    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
'    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
'    a CompareInfo instance associated with the InvariantCulture.

Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, myStr2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "calle" and "calor"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

다음 예제에서는 Compare 메서드를 호출하는 방법을 보여 줍니다.

using namespace System;
using namespace System::Text;
using namespace System::Globalization;

int main()
{
    array<String^>^ sign = gcnew array<String^> { "<", "=", ">" };

    // The code below demonstrates how strings compare
    // differently for different cultures.
    String^ s1 = "Coté"; 
    String^ s2 = "coté";
    String^ s3 = "côte";

    // Set sort order of strings for French in France.
    CompareInfo^ ci = (gcnew CultureInfo("fr-FR"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using fr-FR Compare of Coté = coté.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s1, s2, sign[ci->Compare(s1, s2, CompareOptions::IgnoreCase) + 1]);

    // Display the result using fr-FR Compare of coté > côte.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3, CompareOptions::None) + 1]);

    // Set sort order of strings for Japanese as spoken in Japan.
    ci = (gcnew CultureInfo("ja-JP"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using ja-JP Compare of coté < côte.
    Console::WriteLine("ja-JP Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3) + 1]);
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main(string[] args)
    {
        String[] sign = new String[] { "<", "=", ">" };

        // The code below demonstrates how strings compare
        // differently for different cultures.
        String s1 = "Coté", s2 = "coté", s3 = "côte";

        // Set sort order of strings for French in France.
        CompareInfo ci = new CultureInfo("fr-FR").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s1, s2, sign[ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1]);

        // Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3, CompareOptions.None) + 1]);

        // Set sort order of strings for Japanese as spoken in Japan.
        ci = new CultureInfo("ja-JP").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using ja-JP Compare of coté < côte.
        Console.WriteLine("ja-JP Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3) + 1]);
    }
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
Imports System.Text
Imports System.Globalization

NotInheritable Public Class App
    Shared Sub Main(ByVal args() As String) 
        Dim sign() As String = {"<", "=", ">"}
        
        ' The code below demonstrates how strings compare 
        ' differently for different cultures.
        Dim s1 As String = "Coté"
        Dim s2 As String = "coté"
        Dim s3 As String = "côte"
        
        ' Set sort order of strings for French in France.
        Dim ci As CompareInfo = New CultureInfo("fr-FR").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s1, s2, sign((ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1)))
        
        ' Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3, CompareOptions.None) + 1)))
        
        ' Set sort order of strings for Japanese as spoken in Japan.
        ci = New CultureInfo("ja-JP").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using ja-JP Compare of coté < côte. 
        Console.WriteLine("ja-JP Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3) + 1)))
    End Sub
End Class

' This code produces the following output.
' 
' The LCID for fr-FR is 1036.
' fr-FR Compare: Coté = coté
' fr-FR Compare: coté > côte
' The LCID for ja-JP is 1041.
' ja-JP Compare: coté < côte

설명

기본적으로 비교는 .를 사용하여 CompareOptions.None수행됩니다. 보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 의 매개 변수가 있는 문자열 비교 메서드를 호출하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함되며, 이들 문자는 언어 또는 문화권 구분 비교를 수행하는 경우 고려되지 않습니다. 이 메서드는 Compare(String, String) 문화권 구분 비교를 수행할 때 이러한 문자를 고려하지 않습니다. 예를 들어 "animal"과 "ani-mal"(소프트 하이픈 또는 U+00AD 사용)의 문화권 구분 비교는 다음 예제와 같이 두 문자열이 동일하다는 것을 나타냅니다.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
      Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
      Dim s2 As String = "animal"
      
      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, s2, ci.Compare(s1, s2))
  End Sub
End Module
' The example displays the following output:
'       Comparison of 'ani-mal' and 'animal': 0

문자열 비교에서 무시할 수 있는 문자를 인식하려면 메서드를 Compare(String, String, CompareOptions) 호출하고 매개 변수에 Ordinal OrdinalIgnoreCase 대한 options 값을 입력합니다.

적용 대상

Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

두 개의 읽기 전용 문자 범위를 비교합니다.

public int Compare (ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.Compare : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions -> int
Public Function Compare (string1 As ReadOnlySpan(Of Char), string2 As ReadOnlySpan(Of Char), Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Integer

매개 변수

string1
ReadOnlySpan<Char>

비교할 첫 번째 읽기 전용 문자 범위입니다.

string2
ReadOnlySpan<Char>

비교할 두 번째 읽기 전용 문자 범위입니다.

options
CompareOptions

비교하는 동안 사용할 CompareOptions 열거형 값의 선택적 조합입니다. 기본값은 None입니다.

반환

Int32

string1string2가 같으면 0이고, string1string2 앞에 정렬되는 경우 음수 값이며, string1string2 뒤에 정렬되는 경우 양수 값입니다.

예외

options에 지원되지 않는 플래그 조합이 포함되어 있습니다.

적용 대상

Compare(String, String, CompareOptions)

지정된 CompareOptions 값을 사용하여 두 문자열을 비교합니다.

public:
 virtual int Compare(System::String ^ string1, System::String ^ string2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, System::String ^ string2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, string string2, System.Globalization.CompareOptions options);
public int Compare (string? string1, string? string2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, string? string2, System.Globalization.CompareOptions options);
abstract member Compare : string * string * System.Globalization.CompareOptions -> int
override this.Compare : string * string * System.Globalization.CompareOptions -> int
member this.Compare : string * string * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, string2 As String, options As CompareOptions) As Integer
Public Function Compare (string1 As String, string2 As String, options As CompareOptions) As Integer

매개 변수

string1
String

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

string2
String

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

options
CompareOptions

string1string2을 비교하는 방법을 정의하는 값입니다. options는 열거형 값 Ordinal이거나 IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaTypeStringSort 값 중 하나 이상의 비트 조합입니다.

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 string1string2보다 작은 경우
0보다 큼 string1string2보다 큰 경우

예외

options에 잘못된 CompareOptions 값이 포함된 경우

예제

다음 예제에서는 서로 다른 CompareOptions 설정을 사용하는 두 문자열을 비교합니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo which uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, myStr2 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, myStr2, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "My Uncle Bill's clients" and "My uncle bills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, myStr2, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "My Uncle Bill's clients" and "My uncle bills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, myStr2))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "My Uncle Bill's clients" and "My uncle bills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0

다음 예제에서는 Compare 메서드를 호출하는 방법을 보여 줍니다.

using namespace System;
using namespace System::Text;
using namespace System::Globalization;

int main()
{
    array<String^>^ sign = gcnew array<String^> { "<", "=", ">" };

    // The code below demonstrates how strings compare
    // differently for different cultures.
    String^ s1 = "Coté"; 
    String^ s2 = "coté";
    String^ s3 = "côte";

    // Set sort order of strings for French in France.
    CompareInfo^ ci = (gcnew CultureInfo("fr-FR"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using fr-FR Compare of Coté = coté.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s1, s2, sign[ci->Compare(s1, s2, CompareOptions::IgnoreCase) + 1]);

    // Display the result using fr-FR Compare of coté > côte.
    Console::WriteLine(L"fr-FR Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3, CompareOptions::None) + 1]);

    // Set sort order of strings for Japanese as spoken in Japan.
    ci = (gcnew CultureInfo("ja-JP"))->CompareInfo;
    Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID);

    // Display the result using ja-JP Compare of coté < côte.
    Console::WriteLine("ja-JP Compare: {0} {2} {1}",
        s2, s3, sign[ci->Compare(s2, s3) + 1]);
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main(string[] args)
    {
        String[] sign = new String[] { "<", "=", ">" };

        // The code below demonstrates how strings compare
        // differently for different cultures.
        String s1 = "Coté", s2 = "coté", s3 = "côte";

        // Set sort order of strings for French in France.
        CompareInfo ci = new CultureInfo("fr-FR").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s1, s2, sign[ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1]);

        // Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3, CompareOptions.None) + 1]);

        // Set sort order of strings for Japanese as spoken in Japan.
        ci = new CultureInfo("ja-JP").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using ja-JP Compare of coté < côte.
        Console.WriteLine("ja-JP Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3) + 1]);
    }
}

// This code produces the following output.
//
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte
Imports System.Text
Imports System.Globalization

NotInheritable Public Class App
    Shared Sub Main(ByVal args() As String) 
        Dim sign() As String = {"<", "=", ">"}
        
        ' The code below demonstrates how strings compare 
        ' differently for different cultures.
        Dim s1 As String = "Coté"
        Dim s2 As String = "coté"
        Dim s3 As String = "côte"
        
        ' Set sort order of strings for French in France.
        Dim ci As CompareInfo = New CultureInfo("fr-FR").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s1, s2, sign((ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1)))
        
        ' Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3, CompareOptions.None) + 1)))
        
        ' Set sort order of strings for Japanese as spoken in Japan.
        ci = New CultureInfo("ja-JP").CompareInfo
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID)
        
        ' Display the result using ja-JP Compare of coté < côte. 
        Console.WriteLine("ja-JP Compare: {0} {2} {1}", _
                          s2, s3, sign((ci.Compare(s2, s3) + 1)))
    End Sub
End Class

' This code produces the following output.
' 
' The LCID for fr-FR is 1036.
' fr-FR Compare: Coté = coté
' fr-FR Compare: coté > côte
' The LCID for ja-JP is 1041.
' ja-JP Compare: coté < côte

설명

보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 의 매개 변수가 있는 문자열 비교 메서드를 호출하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함되며, 이들 문자는 언어 또는 문화권 구분 비교를 수행하는 경우 고려되지 않습니다. 이 메서드는 Compare(String, String, CompareOptions) 문화권 구분 비교를 수행할 때 이러한 문자를 고려하지 않습니다. 비교에서 무시할 수 있는 문자를 인식하려면 매개 변수 값 또는 OrdinalIgnoreCase 매개 변수 값을 Ordinal options 입력합니다.

추가 정보

적용 대상

Compare(String, Int32, String, Int32)

한 문자열의 끝 영역을 다른 문자열의 끝 영역과 비교합니다.

public:
 virtual int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2);
public:
 int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2);
public virtual int Compare (string string1, int offset1, string string2, int offset2);
public int Compare (string? string1, int offset1, string? string2, int offset2);
public virtual int Compare (string? string1, int offset1, string? string2, int offset2);
abstract member Compare : string * int * string * int -> int
override this.Compare : string * int * string * int -> int
member this.Compare : string * int * string * int -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer) As Integer
Public Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer) As Integer

매개 변수

string1
String

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

offset1
Int32

비교를 시작할 string1에 있는 문자의 인덱스(0부터 시작)입니다.

string2
String

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

offset2
Int32

비교를 시작할 string2에 있는 문자의 인덱스(0부터 시작)입니다.

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 지정된 string1 영역이 지정된 string2 영역보다 작은 경우
0보다 큼 지정된 string1 영역이 지정된 string2 영역보다 큰 경우

예외

offset1 또는 offset2가 0보다 작습니다.

또는

offset1string1의 문자 수보다 크거나 같은 경우

또는

offset2string2의 문자 수보다 크거나 같은 경우

예제

다음 예제에서는 서로 다른 CompareInfo 개체를 사용하여 두 문자열의 일부를 비교합니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 2 ), myStr2->Substring( 2 ) );
   Console::WriteLine( "   With myCompIntl::Compare: {0}", myCompIntl->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompTrad::Compare: {0}", myCompTrad->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompInva::Compare: {0}", myCompInva->Compare( myStr1, 2, myStr2, 2 ) );
}

/*
This code produces the following output.

Comparing "lle" and "lor"
   With myCompIntl::Compare: -1
   With myCompTrad::Compare: 1
   With myCompInva::Compare: -1
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 2 ), myStr2.Substring( 2 ) );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, 2, myStr2, 2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, 2, myStr2, 2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, 2, myStr2, 2 ) );
   }
}


/*
This code produces the following output.

Comparing "lle" and "lor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(2), myStr2.Substring(2))
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, 2, myStr2, 2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, 2, myStr2, 2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, 2, myStr2, 2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "lle" and "lor"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

설명

보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 매개 변수가 있는 문자열 비교 메서드를 호출하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함됩니다. 이 메서드는 Compare(String, Int32, String, Int32) 언어 또는 문화권 구분 비교를 수행할 때 이러한 문자를 고려하지 않습니다. 비교에서 무시할 수 있는 문자를 인식하려면 메서드를 Compare(String, Int32, String, Int32, CompareOptions) 호출하고 매개 변수 값 또는 OrdinalIgnoreCase options 매개 변수 값을 Ordinal 입력합니다.

적용 대상

Compare(String, Int32, String, Int32, CompareOptions)

지정된 CompareOptions 값을 사용하여 한 문자열의 끝 영역을 다른 문자열의 시작 영역과 비교합니다.

public:
 virtual int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, int offset1, System::String ^ string2, int offset2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, int offset1, string string2, int offset2, System.Globalization.CompareOptions options);
public int Compare (string? string1, int offset1, string? string2, int offset2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, int offset1, string? string2, int offset2, System.Globalization.CompareOptions options);
abstract member Compare : string * int * string * int * System.Globalization.CompareOptions -> int
override this.Compare : string * int * string * int * System.Globalization.CompareOptions -> int
member this.Compare : string * int * string * int * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer, options As CompareOptions) As Integer
Public Function Compare (string1 As String, offset1 As Integer, string2 As String, offset2 As Integer, options As CompareOptions) As Integer

매개 변수

string1
String

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

offset1
Int32

비교를 시작할 string1에 있는 문자의 인덱스(0부터 시작)입니다.

string2
String

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

offset2
Int32

비교를 시작할 string2에 있는 문자의 인덱스(0부터 시작)입니다.

options
CompareOptions

string1string2을 비교하는 방법을 정의하는 값입니다. options는 열거형 값 Ordinal이거나 IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaTypeStringSort 값 중 하나 이상의 비트 조합입니다.

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 지정된 string1 영역이 지정된 string2 영역보다 작은 경우
0보다 큼 지정된 string1 영역이 지정된 string2 영역보다 큰 경우

예외

offset1 또는 offset2가 0보다 작습니다.

또는

offset1string1의 문자 수보다 크거나 같은 경우

또는

offset2string2의 문자 수보다 크거나 같은 경우

options에 잘못된 CompareOptions 값이 포함된 경우

예제

다음 예제에서는 서로 다른 CompareOptions 설정을 사용하여 두 문자열의 일부를 비교합니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 10 ), myStr2->Substring( 10 ) );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, 10, myStr2, 10 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, 10, myStr2, 10, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, 10, myStr2, 10, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "ill's clients" and "ills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -76
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 0
   With IgnoreCase and IgnoreSymbols : 0
*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 10 ), myStr2.Substring( 10 ) );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, 10, myStr2, 10 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "ill's clients" and "ills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -76
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 0
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(10), myStr2.Substring(10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 10, myStr2, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "ill's clients" and "ills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -76
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 0
'   With IgnoreCase and IgnoreSymbols : 0

설명

보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 매개 변수가 있는 문자열 비교 메서드를 호출하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함되며, 이들 문자는 언어 또는 문화권 구분 비교를 수행하는 경우 고려되지 않습니다. 문화권 구분 비교를 수행할 때 메서드는 Compare(String, Int32, String, Int32, CompareOptions) 이러한 문자를 고려하지 않습니다. 비교에서 무시할 수 있는 문자를 인식하려면 매개 변수 값 또는 OrdinalIgnoreCase 매개 변수 값을 Ordinal options 입력합니다.

추가 정보

적용 대상

Compare(String, Int32, Int32, String, Int32, Int32)

한 문자열의 특정 영역을 다른 문자열의 특정 영역과 비교합니다.

public:
 virtual int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2);
public:
 int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2);
public virtual int Compare (string string1, int offset1, int length1, string string2, int offset2, int length2);
public int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2);
public virtual int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2);
abstract member Compare : string * int * int * string * int * int -> int
override this.Compare : string * int * int * string * int * int -> int
member this.Compare : string * int * int * string * int * int -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer) As Integer
Public Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer) As Integer

매개 변수

string1
String

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

offset1
Int32

비교를 시작할 string1에 있는 문자의 인덱스(0부터 시작)입니다.

length1
Int32

비교할 string1의 연속된 문자 수입니다.

string2
String

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

offset2
Int32

비교를 시작할 string2에 있는 문자의 인덱스(0부터 시작)입니다.

length2
Int32

비교할 string2의 연속된 문자 수입니다.

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 지정된 string1 영역이 지정된 string2 영역보다 작은 경우
0보다 큼 지정된 string1 영역이 지정된 string2 영역보다 큰 경우

예외

offset1, length1, offset2 또는 length2가 0보다 작은 경우

또는

offset1string1의 문자 수보다 크거나 같은 경우

또는

offset2string2의 문자 수보다 크거나 같은 경우

또는

length1offset1에서 string1 끝 사이의 문자 수보다 큰 경우

또는

length2offset2에서 string2 끝 사이의 문자 수보다 큰 경우

예제

다음 예제에서는 서로 다른 CompareInfo 개체를 사용하여 두 문자열의 일부를 비교합니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";
   
   // Uses GetCompareInfo to create the CompareInfo that uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );
   
   // Uses GetCompareInfo to create the CompareInfo that uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );
   
   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \" {0}\" and \" {1}\"", myStr1->Substring( 2, 2 ), myStr2->Substring( 2, 2 ) );
   Console::WriteLine( "   With myCompIntl->Compare: {0}", myCompIntl->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   Console::WriteLine( "   With myCompTrad->Compare: {0}", myCompTrad->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   Console::WriteLine( "   With myCompInva->Compare: {0}", myCompInva->Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
}

/*
This code produces the following output.

Comparing S"ll" and S"lo"
With myCompIntl.Compare: -1
With myCompTrad.Compare: 1
With myCompInva.Compare: -1

*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 2, 2 ), myStr2.Substring( 2, 2 ) );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
   }
}


/*
This code produces the following output.

Comparing "ll" and "lo"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(2, 2), myStr2.Substring(2, 2))
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, 2, 2, myStr2, 2, 2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "ll" and "lo"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

설명

보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 매개 변수가 있는 문자열 비교 메서드를 사용하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함됩니다. 이 메서드는 Compare(String, Int32, Int32, String, Int32, Int32) 언어 또는 문화권 구분 비교를 수행할 때 이러한 문자를 고려하지 않습니다. 비교에서 무시할 수 있는 문자를 인식하려면 메서드를 Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) 호출하고 매개 변수 값 또는 OrdinalIgnoreCase options 매개 변수 값을 Ordinal 입력합니다.

적용 대상

Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions)

지정된 CompareOptions 값을 사용하여 한 문자열의 특정 영역을 다른 문자열의 특정 영역과 비교합니다.

public:
 virtual int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2, System::Globalization::CompareOptions options);
public:
 int Compare(System::String ^ string1, int offset1, int length1, System::String ^ string2, int offset2, int length2, System::Globalization::CompareOptions options);
public virtual int Compare (string string1, int offset1, int length1, string string2, int offset2, int length2, System.Globalization.CompareOptions options);
public int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2, System.Globalization.CompareOptions options);
public virtual int Compare (string? string1, int offset1, int length1, string? string2, int offset2, int length2, System.Globalization.CompareOptions options);
abstract member Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
override this.Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
member this.Compare : string * int * int * string * int * int * System.Globalization.CompareOptions -> int
Public Overridable Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer, options As CompareOptions) As Integer
Public Function Compare (string1 As String, offset1 As Integer, length1 As Integer, string2 As String, offset2 As Integer, length2 As Integer, options As CompareOptions) As Integer

매개 변수

string1
String

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

offset1
Int32

비교를 시작할 string1에 있는 문자의 인덱스(0부터 시작)입니다.

length1
Int32

비교할 string1의 연속된 문자 수입니다.

string2
String

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

offset2
Int32

비교를 시작할 string2에 있는 문자의 인덱스(0부터 시작)입니다.

length2
Int32

비교할 string2의 연속된 문자 수입니다.

options
CompareOptions

string1string2을 비교하는 방법을 정의하는 값입니다. options는 열거형 값 Ordinal이거나 IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaTypeStringSort 값 중 하나 이상의 비트 조합입니다.

반환

Int32

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

조건
0 두 문자열이 같은 경우
0보다 작음 지정된 string1 영역이 지정된 string2 영역보다 작은 경우
0보다 큼 지정된 string1 영역이 지정된 string2 영역보다 큰 경우

예외

offset1, length1, offset2 또는 length2가 0보다 작은 경우

또는

offset1string1의 문자 수보다 크거나 같은 경우

또는

offset2string2의 문자 수보다 크거나 같은 경우

또는

length1offset1에서 string1 끝 사이의 문자 수보다 큰 경우

또는

length2offset2에서 string2 끝 사이의 문자 수보다 큰 경우

options에 잘못된 CompareOptions 값이 포함된 경우

예제

다음 예제에서는 서로 다른 CompareOptions 설정을 사용하여 두 문자열의 일부를 비교합니다.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Defines the strings to compare.
   String^ myStr1 = "My Uncle Bill's clients";
   String^ myStr2 = "My uncle bills clients";
   
   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo;
   
   // Compares two strings using myComp.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 3, 10 ), myStr2->Substring( 3, 10 ) );
   Console::WriteLine( "   With no CompareOptions            : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10 ) );
   Console::WriteLine( "   With None                         : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::None ) );
   Console::WriteLine( "   With Ordinal                      : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::Ordinal ) );
   Console::WriteLine( "   With StringSort                   : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::StringSort ) );
   Console::WriteLine( "   With IgnoreCase                   : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreCase ) );
   Console::WriteLine( "   With IgnoreSymbols                : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreSymbols ) );
   Console::WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, 3, 10, myStr2, 3, 10, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) );
}

/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 3, 10 ), myStr2.Substring( 3, 10 ) );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );
   }
}


/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(3, 10), myStr2.Substring(3, 10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "Uncle Bill" and "uncle bill"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : 1
'   With IgnoreCase                   : 0
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0

설명

보안 결정이 문자열 비교 또는 대/소문자 변경에 따라 달라지는 경우 이 속성을 사용하여 InvariantCulture 운영 체제의 문화권 설정에 관계없이 동작이 일관되도록 해야 합니다.

참고

가능하면 형식 CompareOptions 매개 변수가 있는 문자열 비교 메서드를 호출하여 예상되는 비교 종류를 지정해야 합니다. 일반적으로 사용자 인터페이스에 표시된 문자열을 비교하고 보안 비교를 위해 Ordinal OrdinalIgnoreCase 언어 옵션(현재 문화권 사용)을 사용합니다.

호출자 참고

문자 집합에는 무시할 수 있는 문자가 포함됩니다. 이 메서드는 Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) 문화권 구분 비교를 수행할 때 이러한 문자를 고려하지 않습니다. 비교에서 무시할 수 있는 문자를 인식하려면 매개 변수 값 또는 OrdinalIgnoreCase 매개 변수 값을 Ordinal options 입력합니다.

추가 정보

적용 대상