次の方法で共有


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

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

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

パラメータ

  • value
    シークする String
  • startIndex
    検索が開始される位置。
  • count
    検査する文字位置の数。

戻り値

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

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException count または startIndex が負の値です。

または

startIndex に count を足した数が、このインスタンス内にない位置を示しています。

解説

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

このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。 startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、 startIndex + count の文字は検出されませんでした。

使用例

 
' Sample for String.IndexOf(String, Int32, 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
      Dim [end] As Integer
      Dim count As Integer
      
      [end] = str.Length
      start = [end] / 2
      Console.WriteLine()
      Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1)
      Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
      Console.Write("The string 'he' occurs at position(s): ")
      
      count = 0
      at = 0
      While start <= [end] AndAlso at > - 1
         ' start+count must be a position within -str-.
         count = [end] - start
         at = str.IndexOf("he", start, count)
         If at = - 1 Then
            Exit While
         End If
         Console.Write("{0} ", at)
         start = at + 1
      End While
      Console.WriteLine()
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All occurrences of 'he' from position 33 to 66.
'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): 45 56
'
'

[C#] 
// Sample for String.IndexOf(String, Int32, 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;
    int end;
    int count;

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

    count = 0;
    at = 0;
    while((start <= end) && (at > -1))
        {
// start+count must be a position within -str-.
        count = end - start;
        at = str.IndexOf("he", start, count);
        if (at == -1) break;
        Console.Write("{0} ", at);
        start = at+1;
        }
    Console.WriteLine();
    }
}
/*
This example produces the following results:

All occurrences of 'he' from position 33 to 66.
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): 45 56

*/

[C++] 
// Sample for String::IndexOf(String, Int32, 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;
   int end;
   int count;

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

   count = 0;
   at = 0;
   while((start <= end) && (at > -1)) {
      // start+count must be a position within -str-.
      count = end - start;
      at = str->IndexOf(S"he", start, count);
      if (at == -1) break;
      Console::Write(S"{0} ", __box( at));
      start = at+1;
   }
   Console::WriteLine();
}
/*
This example produces the following results:

All occurrences of 'he' from position 33 to 66.
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): 45 56

*/

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