次の方法で共有


String.CompareOrdinal メソッド (String, String)

それぞれの文字列の対応する Char オブジェクトの数値を評価することで、指定した 2 つの String を比較します。

Overloads Public Shared Function CompareOrdinal( _
   ByVal strA As String, _   ByVal strB As String _) As Integer
[C#]
public static int CompareOrdinal(stringstrA,stringstrB);
[C++]
public: static int CompareOrdinal(String* strA,String* strB);
[JScript]
public static function CompareOrdinal(
   strA : String,strB : String) : int;

パラメータ

戻り値

2 つの比較対象値の関係を示す整数値。

アクセス許可 説明
0 より小 strAstrB より小さい。
0 strAstrB は等しい。
0 より大きい値 strAstrB より大きい。

解説

このメソッドは、序数の並べ替え規則を使用して比較を実行します。単語、文字列、序数の並べ替えの詳細については、「 System.Globalization.CompareOptions 」を参照してください。

使用例

 
' Sample for String.CompareOrdinal(String, String)
Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer
      
      Console.WriteLine()
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.")
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
      result = [String].CompareOrdinal(str1, str2)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("String '{0}' is ", str1)
      Console.Write("{0} ", str)
      Console.WriteLine("String '{0}'.", str2)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Compare the numeric values of the corresponding Char objects in each string.
'str1 = 'ABCD', str2 = 'abcd'
'String 'ABCD' is less than String 'abcd'.
'

[C#] 
// Sample for String.CompareOrdinal(String, String)
using System;

class Sample {
    public static void Main() {
    String str1 = "ABCD";
    String str2 = "abcd";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/

[C++] 
// Sample for String::CompareOrdinal(String, String)
#using <mscorlib.dll>

using namespace System;

int main() {
   String*  str1 = S"ABCD";
   String*  str2 = S"abcd";
   String* str;
   int result;

   Console::WriteLine();
   Console::WriteLine(S"Compare the numeric values of the corresponding Char objects in each string.");
   Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
   result = String::CompareOrdinal(str1, str2);
   str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
   Console::Write(S"String '{0}' is ", str1);
   Console::Write(S"{0} ", str);
   Console::WriteLine(S"String '{0}'.", str2);
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/

[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, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | String.CompareOrdinal オーバーロードの一覧 | Int32 | Compare | CompareTo