CompareInfo.Compare メソッド
2 つの文字列を比較します。
オーバーロードの一覧
既定の CompareOptions 値を使用して、2 つの文字列を比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, String) As Integer
指定した CompareOptions 値を使用して、2 つの文字列を比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, String, CompareOptions) As Integer
[C#] public virtual int Compare(string, string, CompareOptions);
[C++] public: virtual int Compare(String*, String*, CompareOptions);
[JScript] public function Compare(String, String, CompareOptions) : int;
文字列の末尾部分と別の文字列の末尾部分とを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, String, Integer) As Integer
[C++] public: virtual int Compare(String*, int, String*, int);
[JScript] public function Compare(String, int, String, int) : int;
指定した CompareOptions 値を使用して、文字列の末尾セクションと別の文字列の末尾セクションとを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, String, Integer, CompareOptions) As Integer
[C#] public virtual int Compare(string, int, string, int, CompareOptions);
[C++] public: virtual int Compare(String*, int, String*, int, CompareOptions);
[JScript] public function Compare(String, int, String, int, CompareOptions) : int;
文字列のセクションと別の文字列のセクションとを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, Integer, String, Integer, Integer) As Integer
[C#] public virtual int Compare(string, int, int, string, int, int);
[C++] public: virtual int Compare(String*, int, int, String*, int, int);
[JScript] public function Compare(String, int, int, String, int, int) : int;
指定した CompareOptions 値を使用して、文字列のセクションと別の文字列のセクションとを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, Integer, String, Integer, Integer, CompareOptions) As Integer
[C#] public virtual int Compare(string, int, int, string, int, int, CompareOptions);
[C++] public: virtual int Compare(String*, int, int, String*, int, int, CompareOptions);
[JScript] public function Compare(String, int, int, String, int, int, CompareOptions) : int;
使用例
[Visual Basic, C#, C++] 次に示すのは、異なる CompareOptions 設定を使用して 2 つの文字列の一部分どうしを比較するコード例です。
[Visual Basic, C#, C++] メモ ここでは、Compare のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
CompareInfo クラス | CompareInfo メンバ | System.Globalization 名前空間