このインスタンスと、指定した String オブジェクトを比較します。
Overloads Public Function CompareTo( _
ByVal strB As String _) As Integer
[C#]
public int CompareTo(stringstrB);
[C++]
public: int CompareTo(String* strB);
[JScript]
public function CompareTo(
strB : String) : int;
パラメータ
- strB
String 。
戻り値
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。
値 | 条件 |
---|---|
0 より小さい値 | このインスタンスは strB よりも小さいです。 |
0 | このインスタンスは strB と等価です。 |
0 より大きい値 | このインスタンスは strB よりも大きいです。
または strB が null 参照 (Visual Basic では Nothing) です。 |
解説
このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 比較を実行します。単語、文字列、序数の並べ替えの詳細については、「 System.Globalization.CompareOptions 」を参照してください。
このメソッドの動作の詳細については、 Compare メソッドの解説を参照してください。
使用例
CompareTo メソッドを別の String と共に使用する方法については、次のコード例を参照してください。
Imports System
Public Class CompareToTest
Public Shared Sub Main()
Dim strFirst As String = "Goodbye"
Dim strSecond As String = "Hello"
Dim strThird As String = "a small string"
Dim strFourth As String = "goodbye"
' compare a string to itself
Console.WriteLine(CompareStrings(strFirst, strFirst))
Console.WriteLine(CompareStrings(strFirst, strSecond))
Console.WriteLine(CompareStrings(strFirst, strThird))
' compare a string to another string that varies only by case
Console.WriteLine(CompareStrings(strFirst, strFourth))
Console.WriteLine(CompareStrings(strFourth, strFirst))
End Sub 'Main
Private Shared Function CompareStrings(str1 As String, str2 As String) As String
Dim cmpVal As Integer = str1.CompareTo(str2)
If cmpVal = 0 Then
' compare the values, using the CompareTo method on the first string
' the values are the same
Return "The strings have the same value!"
Else
If cmpVal > 0 Then
' the first value is greater than the second value
Return "The first string is greater than the second string!"
Else
' the second string is greater than the first string
Return "The second string is greater than the first string!"
End If
End If
End Function 'CompareStrings
End Class 'CompareToTest
[C#]
using System;
public class CompareToTest {
public static void Main() {
string strFirst = "Goodbye";
string strSecond = "Hello";
string strThird = "a small string";
string strFourth = "goodbye";
// compare a string to itself
Console.WriteLine (CompareStrings(strFirst, strFirst));
Console.WriteLine (CompareStrings(strFirst, strSecond));
Console.WriteLine (CompareStrings(strFirst, strThird));
// compare a string to another string that varies only by case
Console.WriteLine (CompareStrings(strFirst, strFourth));
Console.WriteLine (CompareStrings(strFourth, strFirst));
}
private static string CompareStrings( string str1, string str2 ) {
// compare the values, using the CompareTo method on the first string
int cmpVal = str1.CompareTo(str2);
if (cmpVal == 0) // the values are the same
return "The strings have the same value!";
else if (cmpVal > 0) // the first value is greater than the second value
return "The first string is greater than the second string!";
else // the second string is greater than the first string
return "The second string is greater than the first string!";
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
String* CompareStrings(String* str1, String* str2)
{
// compare the values, using the CompareTo method on the first string
int cmpVal = str1->CompareTo(str2);
if (cmpVal == 0) // the values are the same
return S"The strings have the same value!";
else if (cmpVal > 0) // the first value is greater than the second value
return S"The first string is greater than the second string!";
else // the second string is greater than the first string
return S"The second string is greater than the first string!";
}
int main()
{
String* strFirst = S"Goodbye";
String* strSecond = S"Hello";
String* strThird = S"a small String*";
String* strFourth = S"goodbye";
// compare a String* to itself
Console::WriteLine (CompareStrings(strFirst, strFirst));
Console::WriteLine (CompareStrings(strFirst, strSecond));
Console::WriteLine (CompareStrings(strFirst, strThird));
// compare a String* to another String* that varies only by case
Console::WriteLine (CompareStrings(strFirst, strFourth));
Console::WriteLine (CompareStrings(strFourth, strFirst));
}
[JScript]
import System;
public class CompareToTest {
public static function Main() : void {
var strFirst : String = "Goodbye";
var strSecond : String = "Hello";
var strThird : String = "a small string";
var strFourth : String = "goodbye";
// compare a string to itself
Console.WriteLine (CompareStrings(strFirst, strFirst));
Console.WriteLine (CompareStrings(strFirst, strSecond));
Console.WriteLine (CompareStrings(strFirst, strThird));
// compare a string to another string that varies only by case
Console.WriteLine (CompareStrings(strFirst, strFourth));
Console.WriteLine (CompareStrings(strFourth, strFirst));
}
private static function CompareStrings( str1 : String, str2 : String ) : String {
// compare the values, using the CompareTo method on the first string
var cmpVal : int = str1.CompareTo(str2);
if (cmpVal == 0) // the values are the same
return "The strings have the same value!";
else if (cmpVal > 0) // the first value is greater than the second value
return "The first string is greater than the second string!";
else // the second string is greater than the first string
return "The second string is greater than the first string!";
}
}
CompareToTest.Main();
必要条件
プラットフォーム: 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
参照
String クラス | String メンバ | System 名前空間 | String.CompareTo オーバーロードの一覧 | Compare | CompareOrdinal