次の方法で共有


CompareInfo.Compare メソッド

定義

2 つの文字列を比較します。

オーバーロード

名前 説明
Compare(String, String)

2 つの文字列を比較します。

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

2 つの文字の読み取り専用スパンを比較します。

Compare(String, String, CompareOptions)

指定した CompareOptions 値を使用して 2 つの文字列を比較します。

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)

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

2 つの文字列を比較します。

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

比較する 2 番目の文字列。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1string2未満です。
0 より大きい string1string2 より大きい。

次の例では、異なる CompareInfo オブジェクトを使用して、2 つの文字列の一部を比較します。

  • CompareInfo スペイン語 (スペイン) の文化に関連付けられたオブジェクトと国際的な並べ替え
  • CompareInfo スペイン語 (スペイン) 文化に関連付けられたオブジェクトと伝統的な並べ替え
  • CompareInfo に関連付けられているオブジェクト InvariantCulture
// 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 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 を使用) は、次の例に示すように、2 つの文字列が同等であることを示します。

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) メソッドを呼び出し、options パラメーターにOrdinalまたはOrdinalIgnoreCaseのいずれかの値を指定します。

適用対象

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

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

2 つの文字の読み取り専用スパンを比較します。

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>

比較する文字の 2 番目の読み取り専用スパン。

options
CompareOptions

比較中に使用する CompareOptions 列挙値のオプションの組み合わせ。 既定値は None です。

戻り値

string1string2が等しい場合は 0、string1string2の前に並べ替えられる場合は負の値、string2後に並べ替える場合string1正の値。

例外

options には、サポートされていないフラグの組み合わせが含まれています。

適用対象

Compare(String, String, CompareOptions)

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

指定した CompareOptions 値を使用して 2 つの文字列を比較します。

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

比較する 2 番目の文字列。

options
CompareOptions

string1string2の比較方法を定義する値。 options は、列挙値 Ordinalか、 IgnoreCaseIgnoreSymbolsIgnoreNonSpaceIgnoreWidthIgnoreKanaTypeNumericOrderingStringSortのいずれかの値のビットごとの組み合わせです。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1string2未満です。
0 より大きい string1string2 より大きい。

例外

options には無効な CompareOptions 値が含まれています。

次の例では、異なる CompareOptions 設定を使用して 2 つの文字列を比較します。

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 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)メソッドでは、カルチャに依存する比較を実行するときに、このような文字は考慮されません。 比較で無視できる文字を認識するには、options パラメーターにOrdinalまたはOrdinalIgnoreCaseの値を指定します。

こちらもご覧ください

適用対象

Compare(String, Int32, String, Int32)

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

文字列の終了セクションと別の文字列の終了セクションを比較します。

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

比較する 2 番目の文字列。

offset2
Int32

比較を開始する string2 の文字の 0 から始まるインデックス。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1の指定したセクションが、string2の指定したセクションより小さい。
0 より大きい string1の指定したセクションが、string2の指定したセクションより大きい。

例外

offset1 または offset2 が 0 未満です。

-又は-

offset1 は、 string1の文字数以上です。

-又は-

offset2 は、 string2の文字数以上です。

次の例では、異なる CompareInfo オブジェクトを使用して、2 つの文字列の一部を比較します。

  • CompareInfo スペイン語 (スペイン) の文化に関連付けられたオブジェクトと国際的な並べ替え

  • CompareInfo スペイン語 (スペイン) 文化に関連付けられたオブジェクトと伝統的な並べ替え

  • CompareInfo に関連付けられているオブジェクト 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.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) メソッドを呼び出し、options パラメーターにOrdinalまたはOrdinalIgnoreCaseの値を指定します。

適用対象

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

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

指定した 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

比較する 2 番目の文字列。

offset2
Int32

比較を開始する string2 の文字の 0 から始まるインデックス。

options
CompareOptions

string1string2の比較方法を定義する値。 options は、列挙値 Ordinalか、 IgnoreCaseIgnoreSymbolsIgnoreNonSpaceIgnoreWidthIgnoreKanaTypeNumericOrderingStringSortのいずれかの値のビットごとの組み合わせです。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1の指定したセクションが、string2の指定したセクションより小さい。
0 より大きい string1の指定したセクションが、string2の指定したセクションより大きい。

例外

offset1 または offset2 が 0 未満です。

-又は-

offset1 は、 string1の文字数以上です。

-又は-

offset2 は、 string2の文字数以上です。

options には無効な CompareOptions 値が含まれています。

次の例では、異なる CompareOptions 設定を使用して、2 つの文字列の一部を比較します。

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) メソッドでは、カルチャに依存する比較を実行する場合、このような文字は考慮されません。 比較で無視できる文字を認識するには、options パラメーターにOrdinalまたはOrdinalIgnoreCaseの値を指定します。

こちらもご覧ください

適用対象

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

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

ある文字列のセクションを別の文字列のセクションと比較します。

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

比較する 2 番目の文字列。

offset2
Int32

比較を開始する string2 の文字の 0 から始まるインデックス。

length2
Int32

比較する string2 内の連続する文字数。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1の指定したセクションが、string2の指定したセクションより小さい。
0 より大きい string1の指定したセクションが、string2の指定したセクションより大きい。

例外

offset1 または length1 または offset2 または length2 が 0 未満です。

-又は-

offset1 は、 string1の文字数以上です。

-又は-

offset2 は、 string2の文字数以上です。

-又は-

length1 は、 offset1 から string1 の末尾までの文字数を超える値です。

-又は-

length2 は、 offset2 から string2 の末尾までの文字数を超える値です。

次の例では、異なる CompareInfo オブジェクトを使用して、2 つの文字列の一部を比較します。

  • CompareInfo スペイン語 (スペイン) の文化に関連付けられたオブジェクトと国際的な並べ替え

  • CompareInfo スペイン語 (スペイン) 文化に関連付けられたオブジェクトと伝統的な並べ替え

  • CompareInfo に関連付けられているオブジェクト 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.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) メソッドを呼び出し、options パラメーターにOrdinalまたはOrdinalIgnoreCaseの値を指定します。

適用対象

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

ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs
ソース:
CompareInfo.cs

指定した 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

比較する 2 番目の文字列。

offset2
Int32

比較を開始する string2 の文字の 0 から始まるインデックス。

length2
Int32

比較する string2 内の連続する文字数。

options
CompareOptions

string1string2の比較方法を定義する値。 options は、列挙値 Ordinalか、 IgnoreCaseIgnoreSymbolsIgnoreNonSpaceIgnoreWidthIgnoreKanaTypeNumericOrderingStringSortのいずれかの値のビットごとの組み合わせです。

戻り値

2 つの比較の構文関係を示す 32 ビット符号付き整数。

価値 条件
ゼロ 2 つの文字列が等しい。
0 未満 string1の指定したセクションが、string2の指定したセクションより小さい。
0 より大きい string1の指定したセクションが、string2の指定したセクションより大きい。

例外

offset1 または length1 または offset2 または length2 が 0 未満です。

-又は-

offset1 は、 string1の文字数以上です。

-又は-

offset2 は、 string2の文字数以上です。

-又は-

length1 は、 offset1 から string1 の末尾までの文字数を超える値です。

-又は-

length2 は、 offset2 から string2 の末尾までの文字数を超える値です。

options には無効な CompareOptions 値が含まれています。

次の例では、異なる CompareOptions 設定を使用して、2 つの文字列の一部を比較します。

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) メソッドでは、カルチャに依存する比較を実行するときに、これらの文字は考慮されません。 比較で無視できる文字を認識するには、options パラメーターにOrdinalまたはOrdinalIgnoreCaseの値を指定します。

こちらもご覧ください

適用対象