次の方法で共有


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

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

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

パラメータ

  • value
    シークする String
  • startIndex
    検索が開始される位置。

戻り値

その文字列が見つかった場合は、 value のインデックス位置。見つからなかった場合は -1。 valueEmpty の場合、戻り値は startIndex です。

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException startIndex が 0 未満か、インスタンス内にない位置を指定しています。

解説

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

このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。この検索では、インスタンスの startIndex の文字位置から開始し、 value が見つかるか、最初の文字位置に到達するまで、インスタンスの先頭へ向かって逆方向に検索を実行します。

使用例

 
' Sample for String.LastIndexOf(String, 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

      '#3
      start = str.Length - 1
      Console.WriteLine("All occurrences of 'he' from position {0} to 0.", start)
      Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
      Console.Write("The string 'he' occurs at position(s): ")
      
      at = 0
      While start > - 1 And at > - 1
         at = str.LastIndexOf("he", 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 'he' 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 string 'he' occurs at position(s): 56 45 8
'
'

[C#] 
// Sample for String.LastIndexOf(String, 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 'he' from position {0} to 0.", start);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("The string 'he' occurs at position(s): ");

    at = 0;
    while((start > -1) && (at > -1))
        {
        at = str.LastIndexOf("he", 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 'he' 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 string 'he' occurs at position(s): 56 45 8


*/

[C++] 
// Sample for String::LastIndexOf(String, 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 'he' from position {0} to 0.", __box(start));
   Console::WriteLine(S"{0}\n{1}\n{2}\n", br1, br2, str);
   Console::Write(S"The string 'he' occurs at position(s): ");

   at = 0;
   while((start > -1) && (at > -1)) {
      at = str->LastIndexOf(S"he", start);
      if (at > -1) {
         Console::Write(S" {0} ", __box(at));
         start = at - 1;
      }
   }
   Console::WriteLine();
}
/*
This example produces the following results:
All occurrences of 'he' 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 string 'he' occurs at position(s):  56  45  8
*/

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