次の方法で共有


String.LastIndexOf メソッド (Char, Int32)

このインスタンス内で最後に出現する指定 Unicode 文字のインデックス位置をレポートします。検索は、指定した文字位置から開始されます。

Overloads Public Function LastIndexOf( _
   ByVal value As Char, _   ByVal startIndex As Integer _) As Integer
[C#]
public int LastIndexOf(charvalue,intstartIndex);
[C++]
public: int LastIndexOf(__wchar_tvalue,intstartIndex);
[JScript]
public function LastIndexOf(
   value : Char,startIndex : int) : int;

パラメータ

  • value
    シークする Unicode 文字。
  • startIndex
    このインスタンス内の部分文字列の開始位置。

戻り値

その文字が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException startIndex が 0 未満か、このインスタンスの長さより大きい値です。

解説

インデックスの番号付けは 0 から始まります。

このメソッドは、インスタンスの startIndex の文字位置から検索を開始し、value が見つかるか、または最初の文字位置に到達するまで、インスタンスの先頭へ向かって逆方向に検索を実行します。検索では、大文字と小文字が区別されます。

このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、 CompareInfo.LastIndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

使用例

 
' Sample for String.LastIndexOf(Char, Int32)
Imports System 
 _

Class Sample
   
   Public Shared Sub Main()
      
      Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
      Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
      Dim str As String = "Now is the time for all good men to come to the aid of their party."
      Dim start As Integer
      Dim at As Integer
      
      start = str.Length - 1
      Console.WriteLine("All occurrences of 't' from position {0} to 0.", start)
      Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
      Console.Write("The letter 't' occurs at position(s): ")
      
      at = 0
      While start > - 1 And at > - 1
         at = str.LastIndexOf("t"c, start)
         If at > - 1 Then
            Console.Write("{0} ", at)
            start = at - 1
         End If
      End While
      Console.Write("{0}{0}{0}", Environment.NewLine)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'All occurrences of 't' from position 66 to 0.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'The letter 't' occurs at position(s): 64 55 44 41 33 11 7
'

[C#] 
// Sample for String.LastIndexOf(Char, Int32)
using System;

class Sample {
    public static void Main() {

    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
    string str = "Now is the time for all good men to come to the aid of their party.";
    int start;
    int at;

    start = str.Length-1;
    Console.WriteLine("All occurrences of 't' from position {0} to 0.", start);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("The letter 't' occurs at position(s): ");

    at = 0;
    while((start > -1) && (at > -1))
        {
        at = str.LastIndexOf('t', start);
        if (at > -1) 
            {
            Console.Write("{0} ", at);
            start = at - 1;
            }
        }
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
All occurrences of 't' from position 66 to 0.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

The letter 't' occurs at position(s): 64 55 44 41 33 11 7
*/

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

using namespace System;

int main() {
   String* br1 = S"0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
   String* br2 = S"0123456789012345678901234567890123456789012345678901234567890123456";
   String* str = S"Now is the time for all good men to come to the aid of their party.";
   int start;
   int at;

   start = str->Length-1;
   Console::WriteLine(S"All occurrences of 't' from position {0} to 0.", __box(start));
   Console::WriteLine(S"{0}\n{1}\n{2}\n", br1, br2, str);
   Console::Write(S"The letter 't' occurs at position(s): ");

   at = 0;
   while((start > -1) && (at > -1)) {
      at = str->LastIndexOf('t', start);
      if (at > -1) {
         Console::Write(S" {0} ", __box(at));
         start = at - 1;
      }
   }
}
/*
This example produces the following results:
All occurrences of 't' from position 66 to 0.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

The letter 't' occurs at position(s): 64 55 44 41 33 11 7
*/

[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.LastIndexOf オーバーロードの一覧 | Char | Int32 | IndexOf | IndexOfAny | LastIndexOfAny