次の方法で共有


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

指定した CompareOptions 値を使用して、文字列のセクションと別の文字列のセクションとを比較します。

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, _   ByVal options As CompareOptions _) As Integer
[C#]
public virtual int Compare(stringstring1,intoffset1,intlength1,stringstring2,intoffset2,intlength2,CompareOptionsoptions);
[C++]
public: virtual int Compare(String* string1,intoffset1,intlength1,String* string2,intoffset2,intlength2,CompareOptionsoptions);
[JScript]
public function Compare(
   string1 : String,offset1 : int,length1 : int,string2 : String,offset2 : int,length2 : int,options : CompareOptions) : int;

パラメータ

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

戻り値

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

例外

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

または

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

または

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

または

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

または

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

ArgumentException options が有効な CompareOptions 値ではありません。

解説

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

使用例

[Visual Basic, C#, C++] 次に示すのは、異なる CompareOptions 設定を使用して 2 つの文字列の一部分どうしを比較するコード例です。

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

End Class 'SamplesCompareInfo


'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


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

*/

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

int main() {
   // Defines the strings to compare.
   String*  myStr1 = S"My Uncle Bill's clients";
   String*  myStr2 = S"My uncle bills clients";

   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo*  myComp = CultureInfo::InvariantCulture->CompareInfo;

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

*/

[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 オーバーロードの一覧 | CompareOptions