String.CompareTo メソッド
このインスタンスと、指定したオブジェクトを比較します。
オーバーロードの一覧
このインスタンスと、指定した Object を比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function CompareTo(Object) As Integer Implements IComparable.CompareTo
[JScript] public function CompareTo(Object) : int;
このインスタンスと、指定した String オブジェクトを比較します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function CompareTo(String) As Integer
[JScript] public function CompareTo(String) : int;
使用例
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();