次の方法で共有


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

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することで、指定した 2 つの String を比較します。パラメータには、部分文字列の長さと開始位置を指定します。

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

パラメータ

  • strA
    第 1 の String
  • indexA
    strA 内の部分文字列の開始インデックス。
  • strB
    第 2 の String
  • indexB
    strB 内の部分文字列の開始インデックス。
  • length
    比較する各部分文字列の最大文字数。

戻り値

2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。

値型 条件
0 より小 strA 内の部分文字列が、 strB 内の部分文字列より小さいです。
0 これらの部分文字列が等しいか、または length が 0 です。
0 より大 strA 内の部分文字列が strB 内の部分文字列より大きいです。

例外

例外の種類 条件
ArgumentOutOfRangeException indexAstrA. Length より大きい値です。

または

indexBstrB. Length より大きい値です。

または

indexAindexB 、または length が負の値です。

解説

indexAindexB 、および length の各パラメータが負数以外である必要があります。

比較される文字数は、 strA から indexA を引いた長さ、 strB から indexB を引いた長さ、および length のうちで一番小さい値になります。

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

使用例

[Visual Basic, C#, C++] CompareOrdinalCompare が異なる並べ替え順序を使用することを、次の例に示します。

 
Imports System
Imports System.Globalization

Class Test
   
  Public Shared Sub Main(args() As [String])
      Dim strLow As [String] = "abc"
      Dim strCap As [String] = "ABC"
      Dim result As [String] = "equal to "
      Dim x As Integer = 0
      Dim pos As Integer = 1

' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.      
      x = [String].CompareOrdinal(strLow, pos, strCap, pos, 1)
      If x < 0 Then
         result = "less than"
      End If
      If x > 0 Then
         result = "greater than"
      End If

' In U.S. English culture, 'b' is linguistically less than 'B'.
      Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
      
      x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
   End Sub 'Main
End Class 'Test

[C#] 
using System;
using System.Globalization;

class Test 
{
    public static void Main(String[] args) 
    {
    String strLow = "abc";
    String strCap = "ABC";
    String result = "equal to ";
    int x = 0;
    int pos = 1;

// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
    x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
    if (x < 0) result = "less than";
    if (x > 0) result = "greater than";
    Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

// In U.S. English culture, 'b' is linguistically less than 'B'.
    x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
    if (x < 0) result = "less than";
    else if (x > 0) result = "greater than";
    Console.WriteLine("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Globalization;

int main()
{
   String* strLow = S"abc";
   String* strCap = S"ABC";
   String* result = S"equal to ";
   int x = 0;
   int pos = 1;

   // The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
   x = String::CompareOrdinal(strLow, pos, strCap, pos, 1);
   if (x < 0) result = S"less than";
   if (x > 0) result = S"greater than";
   Console::WriteLine(S"CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, __box(pos));
   Console::WriteLine(S"   '{0}' is {1} '{2}'", __box(strLow->Chars[pos]), result, __box(strCap->Chars[pos]));

   // In U.S. English culture, 'b' is linguistically less than 'B'.
   x = String::Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo(S"en-US"));
   if (x < 0) result = S"less than";
   else if (x > 0) result = S"greater than";
   Console::WriteLine(S"Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, __box(pos));
   Console::WriteLine(S"   '{0}' is {1} '{2}'", __box(strLow->Chars[pos]), result, __box(strCap->Chars[pos]));
}

[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 オーバーロードの一覧 | Compare | CompareTo | Int32