String.Compare メソッド (String, Int32, String, Int32, Int32, Boolean)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ indexA As Integer, _ strB As String, _ indexB As Integer, _ length As Integer, _ ignoreCase As Boolean _ ) As Integer
Dim strA As String Dim indexA As Integer Dim strB As String Dim indexB As Integer Dim length As Integer Dim ignoreCase As Boolean Dim returnValue As Integer returnValue = String.Compare(strA, indexA, strB, indexB, length, ignoreCase)
public static int Compare ( string strA, int indexA, string strB, int indexB, int length, bool ignoreCase )
public: static int Compare ( String^ strA, int indexA, String^ strB, int indexB, int length, bool ignoreCase )
public static int Compare ( String strA, int indexA, String strB, int indexB, int length, boolean ignoreCase )
public static function Compare ( strA : String, indexA : int, strB : String, indexB : int, length : int, ignoreCase : boolean ) : int
- strB
第 2 の String。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。


比較する部分文字列は strA に indexA を足した位置、および strB に indexB を足した位置から開始されます。1 番目の部分文字列の長さは strA の長さから indexA を引いた値で、2 番目の部分文字列の長さは strB の長さから indexB を引いた値です。
比較する文字の数は、2 つの部分文字列の長さと length の中で、一番小さい値となります。indexA、indexB、および length の各パラメータが負数以外である必要があります。
比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の部分文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

大文字と小文字の表記だけが異なる 2 つの部分文字列を比較する、2 とおりのコード例を次に示します。1 つ目の例では大文字と小文字の違いを無視して比較し、2 つ目の例では大文字と小文字を区別して比較しています。
' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean) Imports System 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:") result = [String].Compare(str1, 2, str2, 2, 2, True) str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to")) Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1) Console.Write("{0} ", str) Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2) Console.WriteLine() Console.WriteLine("Honor case:") result = [String].Compare(str1, 2, str2, 2, 2, False) str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to")) Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1) Console.Write("{0} ", str) Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2) End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'str1 = 'MACHINE', str2 = 'machine' 'Ignore case: 'Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. ' 'Honor case: 'Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'. '
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean) using System; 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:"); result = String.Compare(str1, 2, str2, 2, 2, true); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); Console.WriteLine(); Console.WriteLine("Honor case:"); result = String.Compare(str1, 2, str2, 2, 2, false); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); } } /* This example produces the following results: str1 = 'MACHINE', str2 = 'machine' Ignore case: Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. Honor case: Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'. */
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean) using namespace System; int 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:" ); result = String::Compare( str1, 2, str2, 2, 2, true ); str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to")); Console::Write( "Substring '{0}' in '{1}' is ", str1->Substring( 2, 2 ), str1 ); Console::Write( " {0} ", str ); Console::WriteLine( "substring '{0}' in '{1}'.", str2->Substring( 2, 2 ), str2 ); Console::WriteLine(); Console::WriteLine( "Honor case:" ); result = String::Compare( str1, 2, str2, 2, 2, false ); str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to")); Console::Write( "Substring '{0}' in '{1}' is ", str1->Substring( 2, 2 ), str1 ); Console::Write( " {0} ", str ); Console::WriteLine( "substring '{0}' in '{1}'.", str2->Substring( 2, 2 ), str2 ); } /* This example produces the following results: str1 = 'MACHINE', str2 = 'machine' Ignore case: Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. Honor case: Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'. */
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean) import System.*; class Sample { public static void main(String[] args) { // 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:"); result = String.Compare(str1, 2, str2, 2, 2, true); str = result < 0 ? "less than" : (result > 0) ? "greater than" : "equal to"; Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); Console.WriteLine(); Console.WriteLine("Honor case:"); result = String.Compare(str1, 2, str2, 2, 2, false); str = result < 0 ? "less than" : (result > 0) ? "greater than" : "equal to"; Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); } //main } //Sample /* This example produces the following results: str1 = 'MACHINE', str2 = 'machine' Ignore case: Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. Honor case: Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, Int32, String, Int32, Int32, Boolean, CultureInfo)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ indexA As Integer, _ strB As String, _ indexB As Integer, _ length As Integer, _ ignoreCase As Boolean, _ culture As CultureInfo _ ) As Integer
Dim strA As String Dim indexA As Integer Dim strB As String Dim indexB As Integer Dim length As Integer Dim ignoreCase As Boolean Dim culture As CultureInfo Dim returnValue As Integer returnValue = String.Compare(strA, indexA, strB, indexB, length, ignoreCase, culture)
public static int Compare ( string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture )
public: static int Compare ( String^ strA, int indexA, String^ strB, int indexB, int length, bool ignoreCase, CultureInfo^ culture )
public static int Compare ( String strA, int indexA, String strB, int indexB, int length, boolean ignoreCase, CultureInfo culture )
public static function Compare ( strA : String, indexA : int, strB : String, indexB : int, length : int, ignoreCase : boolean, culture : CultureInfo ) : int
- strB
第 2 の String。
2 つの比較対象値の関係を示す整数値。


比較する部分文字列は strA に indexA を足した位置、および strB に indexB を足した位置から開始されます。1 番目の部分文字列の長さは strA の長さから indexA を引いた値で、2 番目の部分文字列の長さは strB の長さから indexB を引いた値です。
比較する文字の数は、2 つの部分文字列の長さと length の中で、一番小さい値となります。indexA、indexB、および length の各パラメータが負数以外である必要があります。
比較では、culture パラメータを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の部分文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

異なるカルチャを使用して、2 つの部分文字列を比較するコード例を次に示します。大文字と小文字の違いは無視しています。選択したカルチャによって、文字 'I' の比較方法に違いが生じます。
' 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'. '
// 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'. */
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo) using namespace System; using namespace System::Globalization; int 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, gcnew CultureInfo( "tr-TR" ) ); str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"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) ? (String^)"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'. */
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, // CultureInfo) import System.*; import System.Globalization.*; class Sample { public static void main(String[] args) { // 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.get_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); } //main } //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'. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, Int32, String, Int32, Int32, StringComparison)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ indexA As Integer, _ strB As String, _ indexB As Integer, _ length As Integer, _ comparisonType As StringComparison _ ) As Integer
Dim strA As String Dim indexA As Integer Dim strB As String Dim indexB As Integer Dim length As Integer Dim comparisonType As StringComparison Dim returnValue As Integer returnValue = String.Compare(strA, indexA, strB, indexB, length, comparisonType)
public static int Compare ( string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType )
public: static int Compare ( String^ strA, int indexA, String^ strB, int indexB, int length, StringComparison comparisonType )
public static int Compare ( String strA, int indexA, String strB, int indexB, int length, StringComparison comparisonType )
public static function Compare ( strA : String, indexA : int, strB : String, indexB : int, length : int, comparisonType : StringComparison ) : int
- comparisonType
StringComparison 値の 1 つ。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。


比較する部分文字列は strA に indexA を足した位置、および strB に indexB を足した位置から開始されます。1 番目の部分文字列の長さは strA の長さから indexA を引いた値で、2 番目の部分文字列の長さは strB の長さから indexB を引いた値です。
比較する文字の数は、2 つの部分文字列の長さと length の中で、一番小さい値となります。indexA、indexB、および length の各パラメータが負数以外である必要があります。
comparisonType パラメータは、比較の種類 (現在のカルチャを使用するかインバリアント カルチャを使用するか、比較対象値の大文字と小文字を区別するか、並べ替え規則に単語 (カルチャに依存) を使用するか序数 (カルチャ非依存) を使用するか) を指定します。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の部分文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

' Sample for String.Compare(String, Int32, String, Int32, Int32) Imports System Imports Microsoft.VisualBasic Class Sample Public Shared Sub Main() ' 0123456 Dim str1 As [String] = "machine" Dim str2 As [String] = "device" Dim str As [String] Dim result As Integer Console.WriteLine() Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2) result = [String].Compare(str1, 2, str2, 0, 2) str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to")) Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1) Console.Write("{0} ", str) Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2) End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'str1 = 'machine', str2 = 'device' 'Substring 'ch' in 'machine' is less than substring 'de' in 'device'. '
// Sample for String.Compare(String, Int32, String, Int32, Int32) using System; class Sample { public static void Main() { // 0123456 String str1 = "machine"; String str2 = "device"; String str; int result; Console.WriteLine(); Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2); result = String.Compare(str1, 2, str2, 0, 2); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2); } } /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */
// Sample for String::Compare(String, Int32, String, Int32, Int32) using namespace System; int main() { // 0123456 String^ str1 = "machine"; String^ str2 = "device"; String^ str; int result; Console::WriteLine(); Console::WriteLine( "str1 = '{0}', str2 = '{1}'", str1, str2 ); result = String::Compare( str1, 2, str2, 0, 2 ); str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to")); Console::Write( "Substring '{0}' in ' {1}' is ", str1->Substring( 2, 2 ), str1 ); Console::Write( " {0} ", str ); Console::WriteLine( "substring '{0}' in ' {1}'.", str2->Substring( 0, 2 ), str2 ); } /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */
// Sample for String.Compare(String, Int32, String, Int32, Int32) import System.*; class Sample { public static void main(String[] args) { // 0123456 String str1 = "machine"; String str2 = "device"; String str; int result; Console.WriteLine(); Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2); result = String.Compare(str1, 2, str2, 0, 2); str = result < 0 ? "less than" : (result > 0) ? "greater than" : "equal to"; Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2); } //main } //Sample /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, String)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim strA As String Dim strB As String Dim returnValue As Integer returnValue = String.Compare(strA, strB)
- strB
第 2 の String。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。

比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

Compare メソッドを使用して、2 つの文字列を評価する方法については、次の ReverseStringComparer クラスのコード例を参照してください。
Imports System Imports System.Text Imports System.Collections Public Class SamplesArrayList Public Shared Sub Main() Dim myAL As New ArrayList() ' Creates and initializes a new ArrayList. myAL.Add("Eric") myAL.Add("Mark") myAL.Add("Lance") myAL.Add("Rob") myAL.Add("Kris") myAL.Add("Brad") myAL.Add("Kit") myAL.Add("Bradley") myAL.Add("Keith") myAL.Add("Susan") ' Displays the properties and values of the ArrayList. Console.WriteLine("Count: {0}", myAL.Count) PrintValues("Unsorted", myAL) myAL.Sort() PrintValues("Sorted", myAL) Dim comp as New ReverseStringComparer myAL.Sort(comp) PrintValues("Reverse", myAL) Dim names As String() = CType(myAL.ToArray(GetType(String)), String()) End Sub 'Main Public Shared Sub PrintValues(title As String, myList As IEnumerable) Console.Write("{0,10}: ", title) Dim sb As New StringBuilder() Dim s As String For Each s In myList sb.AppendFormat("{0}, ", s) Next s sb.Remove(sb.Length - 2, 2) Console.WriteLine(sb) End Sub 'PrintValues End Class 'SamplesArrayList Public Class ReverseStringComparer Implements IComparer Function Compare(x As Object, y As Object) As Integer implements IComparer.Compare Dim s1 As String = CStr (x) Dim s2 As String = CStr (y) 'negate the return value to get the reverse order Return - [String].Compare(s1, s2) End Function 'Compare End Class 'ReverseStringComparer
using System; using System.Text; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add("Eric"); myAL.Add("Mark"); myAL.Add("Lance"); myAL.Add("Rob"); myAL.Add("Kris"); myAL.Add("Brad"); myAL.Add("Kit"); myAL.Add("Bradley"); myAL.Add("Keith"); myAL.Add("Susan"); // Displays the properties and values of the ArrayList. Console.WriteLine( "Count: {0}", myAL.Count ); PrintValues ("Unsorted", myAL ); myAL.Sort(); PrintValues("Sorted", myAL ); myAL.Sort(new ReverseStringComparer() ); PrintValues ("Reverse" , myAL ); string [] names = (string[]) myAL.ToArray (typeof(string)); } public static void PrintValues(string title, IEnumerable myList ) { Console.Write ("{0,10}: ", title); StringBuilder sb = new StringBuilder(); foreach (string s in myList) { sb.AppendFormat( "{0}, ", s); } sb.Remove (sb.Length-2,2); Console.WriteLine(sb); } } public class ReverseStringComparer : IComparer { public int Compare (object x, object y) { string s1 = x as string; string s2 = y as string; //negate the return value to get the reverse order return - String.Compare (s1,s2); } }
using namespace System; using namespace System::Text; using namespace System::Collections; ref class ReverseStringComparer: public IComparer { public: virtual int Compare( Object^ x, Object^ y ) { String^ s1 = dynamic_cast<String^>(x); String^ s2 = dynamic_cast<String^>(y); //negate the return value to get the reverse order return -String::Compare( s1, s2 ); } }; void PrintValues( String^ title, IEnumerable^ myList ) { Console::Write( "{0,10}: ", title ); StringBuilder^ sb = gcnew StringBuilder; { IEnumerator^ en = myList->GetEnumerator(); String^ s; while ( en->MoveNext() ) { s = en->Current->ToString(); sb->AppendFormat( "{0}, ", s ); } } sb->Remove( sb->Length - 2, 2 ); Console::WriteLine( sb ); } void main() { // Creates and initializes a new ArrayList. ArrayList^ myAL = gcnew ArrayList; myAL->Add( "Eric" ); myAL->Add( "Mark" ); myAL->Add( "Lance" ); myAL->Add( "Rob" ); myAL->Add( "Kris" ); myAL->Add( "Brad" ); myAL->Add( "Kit" ); myAL->Add( "Bradley" ); myAL->Add( "Keith" ); myAL->Add( "Susan" ); // Displays the properties and values of the ArrayList. Console::WriteLine( "Count: {0}", myAL->Count.ToString() ); PrintValues( "Unsorted", myAL ); myAL->Sort(); PrintValues( "Sorted", myAL ); myAL->Sort( gcnew ReverseStringComparer ); PrintValues( "Reverse", myAL ); array<String^>^names = dynamic_cast<array<String^>^>(myAL->ToArray( String::typeid )); }
import System.*; import System.Text.*; import System.Collections.*; public class SamplesArrayList { public static void main(String[] args) { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add("Eric"); myAL.Add("Mark"); myAL.Add("Lance"); myAL.Add("Rob"); myAL.Add("Kris"); myAL.Add("Brad"); myAL.Add("Kit"); myAL.Add("Bradley"); myAL.Add("Keith"); myAL.Add("Susan"); // Displays the properties and values of the ArrayList. Console.WriteLine("Count: {0}", (Int32)myAL.get_Count()); PrintValues("Unsorted", myAL); myAL.Sort(); PrintValues("Sorted", myAL); myAL.Sort(new ReverseStringComparer()); PrintValues("Reverse", myAL); String names[] = (String[])(myAL.ToArray(String.class.ToType())); } //main public static void PrintValues(String title, IEnumerable myList) { Console.Write("{0,10}: ", title); StringBuilder sb = new StringBuilder(); IEnumerator objEnum = myList.GetEnumerator(); while (objEnum.MoveNext()) { String s = System.Convert.ToString(objEnum.get_Current()); sb.AppendFormat("{0}, ", s); } sb.Remove(sb.get_Length() - 2, 2); Console.WriteLine(sb); } //PrintValues } //SamplesArrayList public class ReverseStringComparer implements IComparer { public int Compare(Object x, Object y) { String s1 = System.Convert.ToString(x); String s2 = System.Convert.ToString(y); //negate the return value to get the reverse order return -String.Compare(s1, s2); } //Compare } //ReverseStringComparer
import System; import System.Text; import System.Collections; public class SamplesArrayList { public static function Main() : void { // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add("Eric"); myAL.Add("Mark"); myAL.Add("Lance"); myAL.Add("Rob"); myAL.Add("Kris"); myAL.Add("Brad"); myAL.Add("Kit"); myAL.Add("Bradley"); myAL.Add("Keith"); myAL.Add("Susan"); // Displays the properties and values of the ArrayList. Console.WriteLine( "Count: {0}", myAL.Count ); PrintValues ("Unsorted", myAL ); myAL.Sort(); PrintValues("Sorted", myAL ); myAL.Sort(new ReverseStringComparer() ); PrintValues ("Reverse" , myAL ); var names : String [] = (String[])(myAL.ToArray (System.String)); } public static function PrintValues(title : String, myList: IEnumerable ) : void { Console.Write ("{0,10}: ", title); var sb : StringBuilder = new StringBuilder(); for (var s : String in myList) { sb.AppendFormat( "{0}, ", s); } sb.Remove (sb.Length-2,2); Console.WriteLine(sb); } } public class ReverseStringComparer implements IComparer { public function Compare (x, y) : int { //negate the return value to get the reverse order return - String.Compare (String(x), String(y)); } } SamplesArrayList.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, String, StringComparison)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ strB As String, _ comparisonType As StringComparison _ ) As Integer
Dim strA As String Dim strB As String Dim comparisonType As StringComparison Dim returnValue As Integer returnValue = String.Compare(strA, strB, comparisonType)
public static function Compare ( strA : String, strB : String, comparisonType : StringComparison ) : int
- comparisonType
System.StringComparison 値の 1 つ。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。


comparisonType パラメータは、比較の種類 (現在のカルチャを使用するかインバリアント カルチャを使用するか、比較対象値の大文字と小文字を区別するか、並べ替え規則に単語 (カルチャに依存) を使用するか序数 (カルチャ非依存) を使用するか) を指定します。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

3 種類の 'I' 文字を比較するコード例を次に示します。選択したカルチャ、大文字と小文字の区別、比較の種類 (序数ベースにしたかどうか) が実行結果に影響します。
' This example demonstrates the ' System.String.Compare(String, String, StringComparison) method. Imports System Imports System.Threading Class Sample Public Shared Sub Main() Dim intro As String = "Compare three versions of the letter I using different " & _ "values of StringComparison." ' Define an array of strings where each element contains a version of the ' letter I. (An array of strings is used so you can easily modify this ' code example to test additional or different combinations of strings.) Dim threeIs(2) As String ' LATIN SMALL LETTER I (U+0069) threeIs(0) = "i" ' LATIN SMALL LETTER DOTLESS I (U+0131) threeIs(1) = "ı" ' LATIN CAPITAL LETTER I (U+0049) threeIs(2) = "I" Dim unicodeNames As String() = { _ "LATIN SMALL LETTER I (U+0069)", _ "LATIN SMALL LETTER DOTLESS I (U+0131)", _ "LATIN CAPITAL LETTER I (U+0049)" } Dim scValues As StringComparison() = { _ StringComparison.CurrentCulture, _ StringComparison.CurrentCultureIgnoreCase, _ StringComparison.InvariantCulture, _ StringComparison.InvariantCultureIgnoreCase, _ StringComparison.Ordinal, _ StringComparison.OrdinalIgnoreCase } ' Console.Clear() Console.WriteLine(intro) ' Display the current culture because the culture-specific comparisons ' can produce different results with different cultures. Console.WriteLine("The current culture is {0}." & vbCrLf, _ Thread.CurrentThread.CurrentCulture.Name) ' Determine the relative sort order of three versions of the letter I. Dim sc As StringComparison For Each sc In scValues Console.WriteLine("StringComparison.{0}:", sc) ' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131) Test(0, 1, sc, threeIs, unicodeNames) ' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049) Test(0, 2, sc, threeIs, unicodeNames) ' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049) Test(1, 2, sc, threeIs, unicodeNames) Console.WriteLine() Next sc End Sub 'Main Protected Shared Sub Test(ByVal x As Integer, ByVal y As Integer, _ ByVal comparison As StringComparison, _ ByVal testI() As String, ByVal testNames() As String) Dim resultFmt As String = "{0} is {1} {2}" Dim result As String = "equal to" Dim cmpValue As Integer = 0 ' cmpValue = String.Compare(testI(x), testI(y), comparison) If cmpValue < 0 Then result = "less than" ElseIf cmpValue > 0 Then result = "greater than" End If Console.WriteLine(resultFmt, testNames(x), result, testNames(y)) End Sub 'Test End Class 'Sample ' 'This code example produces the following results: ' 'Compare three versions of the letter I using different values of StringComparison. 'The current culture is en-US. ' 'StringComparison.CurrentCulture: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.CurrentCultureIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.InvariantCulture: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.InvariantCultureIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.Ordinal: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.OrdinalIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) '
// This example demonstrates the // System.String.Compare(String, String, StringComparison) method. using System; using System.Threading; class Sample { public static void Main() { string intro = "Compare three versions of the letter I using different " + "values of StringComparison."; // Define an array of strings where each element contains a version of the // letter I. (An array of strings is used so you can easily modify this // code example to test additional or different combinations of strings.) string[] threeIs = new string[3]; // LATIN SMALL LETTER I (U+0069) threeIs[0] = "\u0069"; // LATIN SMALL LETTER DOTLESS I (U+0131) threeIs[1] = "\u0131"; // LATIN CAPITAL LETTER I (U+0049) threeIs[2] = "\u0049"; string[] unicodeNames = { "LATIN SMALL LETTER I (U+0069)", "LATIN SMALL LETTER DOTLESS I (U+0131)", "LATIN CAPITAL LETTER I (U+0049)" }; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; // Console.Clear(); Console.WriteLine(intro); // Display the current culture because the culture-specific comparisons // can produce different results with different cultures. Console.WriteLine("The current culture is {0}.\n", Thread.CurrentThread.CurrentCulture.Name); // Determine the relative sort order of three versions of the letter I. foreach (StringComparison sc in scValues) { Console.WriteLine("StringComparison.{0}:", sc); // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131) Test(0, 1, sc, threeIs, unicodeNames); // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049) Test(0, 2, sc, threeIs, unicodeNames); // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049) Test(1, 2, sc, threeIs, unicodeNames); Console.WriteLine(); } } protected static void Test(int x, int y, StringComparison comparison, string[] testI, string[] testNames) { string resultFmt = "{0} is {1} {2}"; string result = "equal to"; int cmpValue = 0; // cmpValue = String.Compare(testI[x], testI[y], comparison); if (cmpValue < 0) result = "less than"; else if (cmpValue > 0) result = "greater than"; Console.WriteLine(resultFmt, testNames[x], result, testNames[y]); } } /* This code example produces the following results: Compare three versions of the letter I using different values of StringComparison. The current culture is en-US. StringComparison.CurrentCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.CurrentCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.OrdinalIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) */
// This example demonstrates the // System.String.Compare(String, String, StringComparison) method. using namespace System; using namespace System::Threading; void Test(int testStringIndex, int searchStringIndex, StringComparison comparison, array<String^>^ testI, array<String^>^ testNames) { String^ resultFormat = "{0} is {1} {2}"; String^ resultString = "equal to"; int comparisonValue = 0; comparisonValue = String::Compare(testI[testStringIndex], testI[searchStringIndex], comparison); if (comparisonValue < 0) { resultString = "less than"; } else if (comparisonValue > 0) { resultString = "greater than"; } Console::WriteLine(resultFormat, testNames[testStringIndex], resultString, testNames[searchStringIndex]); } int main() { String^ introMessage = "Compare three versions of the letter I using different " + "values of StringComparison."; // Define an array of strings where each element contains a version of // the letter I. (An array of strings is used so you can easily modify // this code example to test additional or different combinations of // strings.) array<String^>^ letterVariation = gcnew array<String^>(3); // LATIN SMALL LETTER I (U+0069) letterVariation[0] = "i"; // LATIN SMALL LETTER DOTLESS I (U+0131) letterVariation[1] = L"\u0131"; // LATIN CAPITAL LETTER I (U+0049) letterVariation[2] = "I"; array<String^>^ unicodeNames = { "LATIN SMALL LETTER I (U+0069)", "LATIN SMALL LETTER DOTLESS I (U+0131)", "LATIN CAPITAL LETTER I (U+0049)"}; array<StringComparison>^ comparisonValues = { StringComparison::CurrentCulture, StringComparison::CurrentCultureIgnoreCase, StringComparison::InvariantCulture, StringComparison::InvariantCultureIgnoreCase, StringComparison::Ordinal, StringComparison::OrdinalIgnoreCase}; Console::Clear(); Console::WriteLine(introMessage); // Display the current culture because the culture-specific comparisons // can produce different results with different cultures. Console::WriteLine("The current culture is {0}.{1}", Thread::CurrentThread->CurrentCulture->Name, Environment::NewLine); // Determine the relative sort order of three versions of the letter I. for each (StringComparison stringCmp in comparisonValues) { Console::WriteLine("StringComparison.{0}:", stringCmp); // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I // (U+0131) Test(0, 1, stringCmp, letterVariation, unicodeNames); // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049) Test(0, 2, stringCmp, letterVariation, unicodeNames); // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I // (U+0049) Test(1, 2, stringCmp, letterVariation, unicodeNames); Console::WriteLine(); } } /* This code example produces the following results: Compare three versions of the letter I using different values of StringComparison. The current culture is en-US. StringComparison.CurrentCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.CurrentCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.OrdinalIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, String, Boolean, CultureInfo)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ strB As String, _ ignoreCase As Boolean, _ culture As CultureInfo _ ) As Integer
Dim strA As String Dim strB As String Dim ignoreCase As Boolean Dim culture As CultureInfo Dim returnValue As Integer returnValue = String.Compare(strA, strB, ignoreCase, culture)
public static function Compare ( strA : String, strB : String, ignoreCase : boolean, culture : CultureInfo ) : int
- strB
第 2 の String。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。


比較では、culture パラメータを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

カルチャが比較に影響を及ぼすコード例を次に示します。チェコ語 - チェコ共和国のカルチャでは、'ch' は 'd' よりも大きい単一の文字です。しかし、英語 - 米国のカルチャでは、"ch" は 2 つの文字から成り、'c' は 'd' より小さくなります。
Imports System Imports System.Globalization _ Class Sample Public Shared Sub Main() Dim str1 As [String] = "change" Dim str2 As [String] = "dollar" Dim relation As [String] = Nothing relation = symbol([String].Compare(str1, str2, False, New CultureInfo("en-US"))) Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2) relation = symbol([String].Compare(str1, str2, False, New CultureInfo("cs-CZ"))) Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2) End Sub 'Main Private Shared Function symbol(r As Integer) As [String] Dim s As [String] = "=" If r < 0 Then s = "<" Else If r > 0 Then s = ">" End If End If Return s End Function 'symbol End Class 'Sample ' 'This example produces the following results. 'For en-US: change < dollar 'For cs-CZ: change > dollar '
using System; using System.Globalization; class Sample { public static void Main() { String str1 = "change"; String str2 = "dollar"; String relation = null; relation = symbol( String.Compare(str1, str2, false, new CultureInfo("en-US")) ); Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2); relation = symbol( String.Compare(str1, str2, false, new CultureInfo("cs-CZ")) ); Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2); } private static String symbol(int r) { String s = "="; if (r < 0) s = "<"; else if (r > 0) s = ">"; return s; } } /* This example produces the following results. For en-US: change < dollar For cs-CZ: change > dollar */
using namespace System; using namespace System::Globalization; String^ symbol( int r ) { String^ s = "="; if ( r < 0 ) s = "<"; else if ( r > 0 ) s = ">"; return s; } int main() { String^ str1 = "change"; String^ str2 = "dollar"; String^ relation = nullptr; relation = symbol( String::Compare( str1, str2, false, gcnew CultureInfo( "en-US" ) ) ); Console::WriteLine( "For en-US: {0} {1} {2}", str1, relation, str2 ); relation = symbol( String::Compare( str1, str2, false, gcnew CultureInfo( "cs-CZ" ) ) ); Console::WriteLine( "For cs-CZ: {0} {1} {2}", str1, relation, str2 ); } /* This example produces the following results. For en-US: change < dollar For cs-CZ: change > dollar */
import System.*; import System.Globalization.*; class Sample { public static void main(String[] args) { String str1 = "change"; String str2 = "dollar"; String relation = null; relation = Symbol(String.Compare(str1, str2, false, new CultureInfo("en-US"))); Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2); relation = Symbol(String.Compare(str1, str2, false, new CultureInfo("cs-CZ"))); Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2); } //main private static String Symbol(int r) { String s = "="; if (r < 0) { s = "<"; } else { if (r > 0) { s = ">"; } } return s; } //Symbol } //Sample /* This example produces the following results. For en-US: change < dollar For cs-CZ: change > dollar */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド


String.Compare メソッド (String, Int32, String, Int32, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ indexA As Integer, _ strB As String, _ indexB As Integer, _ length As Integer _ ) As Integer
Dim strA As String Dim indexA As Integer Dim strB As String Dim indexB As Integer Dim length As Integer Dim returnValue As Integer returnValue = String.Compare(strA, indexA, strB, indexB, length)
public static function Compare ( strA : String, indexA : int, strB : String, indexB : int, length : int ) : int
- strB
第 2 の String。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。


比較する部分文字列は strA に indexA を足した位置、および strB に indexB を足した位置から開始されます。1 番目の部分文字列の長さは strA の長さから indexA を引いた値で、2 番目の部分文字列の長さは strB の長さから indexB を引いた値です。
比較する文字の数は、2 つの部分文字列の長さと length の中で、一番小さい値となります。indexA、indexB、および length の各パラメータが負数以外である必要があります。
比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の部分文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

' Sample for String.Compare(String, Int32, String, Int32, Int32) Imports System Imports Microsoft.VisualBasic Class Sample Public Shared Sub Main() ' 0123456 Dim str1 As [String] = "machine" Dim str2 As [String] = "device" Dim str As [String] Dim result As Integer Console.WriteLine() Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2) result = [String].Compare(str1, 2, str2, 0, 2) str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to")) Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1) Console.Write("{0} ", str) Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2) End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'str1 = 'machine', str2 = 'device' 'Substring 'ch' in 'machine' is less than substring 'de' in 'device'. '
// Sample for String.Compare(String, Int32, String, Int32, Int32) using System; class Sample { public static void Main() { // 0123456 String str1 = "machine"; String str2 = "device"; String str; int result; Console.WriteLine(); Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2); result = String.Compare(str1, 2, str2, 0, 2); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2); } } /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */
// Sample for String::Compare(String, Int32, String, Int32, Int32) using namespace System; int main() { // 0123456 String^ str1 = "machine"; String^ str2 = "device"; String^ str; int result; Console::WriteLine(); Console::WriteLine( "str1 = '{0}', str2 = '{1}'", str1, str2 ); result = String::Compare( str1, 2, str2, 0, 2 ); str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to")); Console::Write( "Substring '{0}' in ' {1}' is ", str1->Substring( 2, 2 ), str1 ); Console::Write( " {0} ", str ); Console::WriteLine( "substring '{0}' in ' {1}'.", str2->Substring( 0, 2 ), str2 ); } /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */
// Sample for String.Compare(String, Int32, String, Int32, Int32) import System.*; class Sample { public static void main(String[] args) { // 0123456 String str1 = "machine"; String str2 = "device"; String str; int result; Console.WriteLine(); Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2); result = String.Compare(str1, 2, str2, 0, 2); str = result < 0 ? "less than" : (result > 0) ? "greater than" : "equal to"; Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2); } //main } //Sample /* This example produces the following results: str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Compare メソッド (String, String, Boolean)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Compare ( _ strA As String, _ strB As String, _ ignoreCase As Boolean _ ) As Integer
Dim strA As String Dim strB As String Dim ignoreCase As Boolean Dim returnValue As Integer returnValue = String.Compare(strA, strB, ignoreCase)
- strB
第 2 の String。
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。

比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「System.Globalization.CompareOptions」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。

文字列を比較する場合に Compare メソッドを使用しても、ToUpper または ToLower を使用しても同じであることを次のコード例で示します。
unsafe { // Null terminated ASCII characters in an sbyte array String szAsciiUpper = null; sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 }; // Instruct the Garbage Collector not to move the memory fixed(sbyte* pAsciiUpper = sbArr1) { szAsciiUpper = new String(pAsciiUpper); } String szAsciiLower = null; sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 }; // Instruct the Garbage Collector not to move the memory fixed(sbyte* pAsciiLower = sbArr2) { szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length); } // Prints "ABC abc" Console.WriteLine(szAsciiUpper + " " + szAsciiLower); // Compare Strings - the result is true Console.WriteLine("The Strings are equal when capitalized ? " + (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") ); // This is the effective equivalent of another Compare method, which ignores case Console.WriteLine("The Strings are equal when capitalized ? " + (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") ); }
// Null terminated ASCII characters in a simple char array char charArray3[4] = {0x41,0x42,0x43,0x00}; char * pstr3 = &charArray3[ 0 ]; String^ szAsciiUpper = gcnew String( pstr3 ); char charArray4[4] = {0x61,0x62,0x63,0x00}; char * pstr4 = &charArray4[ 0 ]; String^ szAsciiLower = gcnew String( pstr4,0,sizeof(charArray4) ); // Prints "ABC abc" Console::WriteLine( String::Concat( szAsciiUpper, " ", szAsciiLower ) ); // Compare Strings - the result is true Console::WriteLine( String::Concat( "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper->ToUpper(), szAsciiLower->ToUpper() ) ? (String^)"TRUE" : "FALSE") ) ); // This is the effective equivalent of another Compare method, which ignores case Console::WriteLine( String::Concat( "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper, szAsciiLower, true ) ? (String^)"TRUE" : "FALSE") ) );

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からString.Compareを検索する場合は、下記のリンクをクリックしてください。

- String.Compareのページへのリンク