次の方法で共有


CompareInfo.Compare メソッド (String, Int32, Int32, String, Int32, Int32)

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

Overloads Public Overridable Function Compare( _
   ByVal string1 As String, _   ByVal offset1 As Integer, _   ByVal length1 As Integer, _   ByVal string2 As String, _   ByVal offset2 As Integer, _   ByVal length2 As Integer _) As Integer
[C#]
public virtual int Compare(stringstring1,intoffset1,intlength1,stringstring2,intoffset2,intlength2);
[C++]
public: virtual int Compare(String* string1,intoffset1,intlength1,String* string2,intoffset2,intlength2);
[JScript]
public function Compare(
   string1 : String,offset1 : int,length1 : int,string2 : String,offset2 : int,length2 : int) : int;

パラメータ

  • string1
    比較対象の第 1 文字列。
  • offset1
    string1 内の比較を開始する位置にある文字の 0 から始まるインデックス。
  • length1
    比較対象の string1 に含まれる連続する文字の数。
  • string2
    比較対象の第 2 文字列。
  • offset2
    string2 内の比較を開始する位置にある文字の 0 から始まるインデックス。
  • length2
    比較対象の string2 に含まれる連続する文字の数。

戻り値

条件
0 2 つの文字列は同じです。
0 より小さい値 string1 の指定したセクションが string2 の指定したセクションより小さくなっています。
0 より大きい値 string1 の指定したセクションが string2 の指定したセクションより大きくなっています。

例外

例外の種類 条件
ArgumentOutOfRangeException offset1length1offset2 、または length2 が 0 未満です。

または

offset1string1 に含まれている文字数以上の値です。

または

offset2string2 に含まれている文字数以上の値です。

または

length1 が、 offset1 から string1 の末尾までの文字数を超えています。

または

length2 が、 offset2 から string2 の末尾までの文字数を超えています。

解説

セキュリティの決定が文字列の比較や大文字/小文字の変換操作に依存する場合は、システムのカルチャ設定にかかわらず一定の動作を保証するために InvariantCulture を使用してください。

使用例

[Visual Basic, C#, C++] 次に示すのは、複数の異なる CompareInfo インスタンスを使用して、2 つの文字列の一部分どうしを比較するコード例です。使用するインスタンスは、"スペイン語 - スペイン" カルチャの国際対応並べ替え順序を使用した CompareInfo インスタンス、"スペイン語 - スペイン" カルチャの従来の並べ替え順序を使用した CompareInfo インスタンス、および InvariantCulture を使用した CompareInfo インスタンスです。

 
Imports System
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 'Main 

End Class 'SamplesCompareInfo


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


[C#] 
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

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main() {
   // Defines the strings to compare.
   String*  myStr1 = S"calle";
   String*  myStr2 = S"calor";

   // Uses GetCompareInfo to create the CompareInfo that uses the S"es-ES" culture with international sort.
   CompareInfo*  myCompIntl = CompareInfo::GetCompareInfo(S"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(S"Comparing \" {0}\" and \" {1}\"", 
      myStr1->Substring(2, 2), myStr2->Substring(2, 2));
      Console::WriteLine(S"   With myCompIntl->Compare: {0}", 
         __box(myCompIntl->Compare(myStr1, 2, 2, myStr2, 2, 2)));
   Console::WriteLine(S"   With myCompTrad->Compare: {0}", 
      __box(myCompTrad->Compare(myStr1, 2, 2, myStr2, 2, 2)));
   Console::WriteLine(S"   With myCompInva->Compare: {0}", 
      __box(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

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

CompareInfo クラス | CompareInfo メンバ | System.Globalization 名前空間 | CompareInfo.Compare オーバーロードの一覧