指定した CompareOptions 値を使用して、2 つの文字列を比較します。
Overloads Public Overridable Function Compare( _
ByVal string1 As String, _ ByVal string2 As String, _ ByVal options As CompareOptions _) As Integer
[C#]
public virtual int Compare(stringstring1,stringstring2,CompareOptionsoptions);
[C++]
public: virtual int Compare(String* string1,String* string2,CompareOptionsoptions);
[JScript]
public function Compare(
string1 : String,string2 : String,options : CompareOptions) : int;
パラメータ
- string1
比較対象の第 1 文字列。 - string2
比較対象の第 2 文字列。 - options
文字列の比較方法を定義する CompareOptions 値。
戻り値
| 値 | 条件 |
|---|---|
| 0 | 2 つの文字列は同じです。 |
| 0 より小さい値 | string1 が string2 より小さい値です。 |
| 0 より大きい値 | string1 が 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, 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 'Main
End Class 'SamplesCompareInfo
'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
[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, 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
*/
[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 which uses the InvariantCulture.
CompareInfo* myComp = CultureInfo::InvariantCulture->CompareInfo;
// Compares two strings using myComp.
Console::WriteLine(S"Comparing \"{0}\" and \"{1}\"", myStr1, myStr2);
Console::WriteLine(S" With no CompareOptions : {0}",
__box(myComp->Compare(myStr1, myStr2)));
Console::WriteLine(S" With None : {0}",
__box(myComp->Compare(myStr1, myStr2, CompareOptions::None)));
Console::WriteLine(S" With Ordinal : {0}",
__box(myComp->Compare(myStr1, myStr2, CompareOptions::Ordinal)));
Console::WriteLine(S" With StringSort : {0}",
__box(myComp->Compare(myStr1, myStr2, CompareOptions::StringSort)));
Console::WriteLine(S" With IgnoreCase : {0}",
__box(myComp->Compare(myStr1, myStr2, CompareOptions::IgnoreCase)));
Console::WriteLine(S" With IgnoreSymbols : {0}",
__box(myComp->Compare(myStr1, myStr2, CompareOptions::IgnoreSymbols)));
Console::WriteLine(S" With IgnoreCase and IgnoreSymbols : {0}",
__box(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
*/
[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