String.Compare メソッド
指定した 2 つの String オブジェクトを比較します。
オーバーロードの一覧
指定した 2 つの String オブジェクトを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, String) As Integer
[JScript] public static function Compare(String, String) : int;
指定した 2 つの String オブジェクトを比較します。比較時に大文字小文字を区別するかどうかを設定できます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, String, Boolean) As Integer
[JScript] public static function Compare(String, String, Boolean) : int;
指定した 2 つの String オブジェクトを比較します。大文字と小文字を区別するかどうかの指定と、比較に影響を及ぼすカルチャ固有の情報を使用します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, String, Boolean, CultureInfo) As Integer
[C#] public static int Compare(string, string, bool, CultureInfo);
[C++] public: static int Compare(String*, String*, bool, CultureInfo*);
[JScript] public static function Compare(String, String, Boolean, CultureInfo) : int;
指定した 2 つの String オブジェクトの部分文字列を比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer) As Integer
[C#] public static int Compare(string, int, string, int, int);
[C++] public: static int Compare(String*, int, String*, int, int);
[JScript] public static function Compare(String, int, String, int, int) : int;
指定した 2 つの String オブジェクトの部分文字列を比較します。比較時に、大文字小文字を区別するかどうかを設定できます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer, Boolean) As Integer
[C#] public static int Compare(string, int, string, int, int, bool);
[C++] public: static int Compare(String*, int, String*, int, int, bool);
[JScript] public static function Compare(String, int, String, int, int, Boolean) : int;
指定した 2 つの String オブジェクトの部分文字列を比較します。大文字と小文字を区別するかどうかの指定と、比較に影響を及ぼすカルチャ固有の情報を使用します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer, Boolean, CultureInfo) As Integer
[C#] public static int Compare(string, int, string, int, int, bool, CultureInfo);
[C++] public: static int Compare(String*, int, String*, int, int, bool, CultureInfo*);
[JScript] public static function Compare(String, int, String, int, int, Boolean, CultureInfo) : int;
使用例
[Visual Basic, C#, C++] メモ ここでは、Compare のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "MACHINE"
Dim str2 As [String] = "machine"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
Console.WriteLine("Ignore case, Turkish culture:")
result = [String].Compare(str1, 4, str2, 4, 2, True, New CultureInfo("tr-TR"))
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
Console.WriteLine()
Console.WriteLine("Ignore case, invariant culture:")
result = [String].Compare(str1, 4, str2, 4, 2, True, CultureInfo.InvariantCulture)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'str1 = 'MACHINE', str2 = 'machine'
'Ignore case, Turkish culture:
'Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.
'
'Ignore case, invariant culture:
'Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
'
[C#]
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
using System;
using System.Globalization;
class Sample {
public static void Main() {
// 0123456
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case, Turkish culture:");
result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
Console.WriteLine();
Console.WriteLine("Ignore case, invariant culture:");
result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
}
}
/*
This example produces the following results:
str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.
Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/
[C++]
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main() {
// 0123456
String* str1 = S"MACHINE";
String* str2 = S"machine";
String* str;
int result;
Console::WriteLine();
Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
Console::WriteLine(S"Ignore case, Turkish culture:");
result = String::Compare(str1, 4, str2, 4, 2, true, new CultureInfo(S"tr-TR"));
str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
Console::Write(S"Substring '{0}' in '{1}' is ", str1->Substring(4, 2), str1);
Console::Write(S" {0} ", str);
Console::WriteLine(S"substring '{0}' in '{1}'.", str2->Substring(4, 2), str2);
Console::WriteLine();
Console::WriteLine(S"Ignore case, invariant culture:");
result = String::Compare(str1, 4, str2, 4, 2, true, CultureInfo::InvariantCulture);
str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
Console::Write(S"Substring '{0}' in '{1}' is ", str1->Substring(4, 2), str1);
Console::Write(S" {0} ", str);
Console::WriteLine(S"substring '{0}' in '{1}'.", str2->Substring(4, 2), str2);
}
/*
This example produces the following results:
str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.
Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。