指定された文字列がこのインスタンス内で最初に見つかった位置のインデックスをレポートします。 検索は、指定した文字位置から開始されます。
アセンブリ: mscorlib (mscorlib.dll 内)
Public Function IndexOf ( _ value As String, _ startIndex As Integer _ ) As Integer
public int IndexOf( string value, int startIndex )
public: int IndexOf( String^ value, int startIndex )
member IndexOf : value:string * startIndex:int -> int
戻り値
型: System.Int32その文字列が見つかった場合は、value の 0 から始まるインデックスでの位置。見つからなかった場合は -1。 value が String.Empty の場合、戻り値は startIndex です。
| 例外 | 条件 |
|---|---|
| ArgumentNullException |
value は null なので、 |
| ArgumentOutOfRangeException |
startIndex が負の値です。 または startIndex が、このインスタンス内にない位置を指定しています。 |
インデックスの番号付けは 0 から始まります。 startIndex パラメーターには、0 から文字列インスタンスの長さより 1 小さい値までの範囲内の値を指定できます。
このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。 このインスタンスの startIndex で指定される文字位置から最後の文字位置まで検索されます。
呼び出し時の注意
「.NET Framework で文字列を使用するためのベスト プラクティス」で説明しているように、既定値を代入する文字列比較メソッドの呼び出さず、代わりにパラメーターを明示的に指定する必要があるメソッドを呼び出すことをお勧めします。 現在のカルチャの文字列比較の規則を使用して特定の文字の位置の後に発生する部分文字列の最初のインデックスを検索するには、その comparisonType パラメーターの StringComparison.CurrentCulture 値で IndexOf(String, Int32, StringComparison) メソッド オーバーロードを呼び出します。
検索対象の文字列内で、指定した文字列をすべて検索する例を次に示します。
Imports System Public Class IndexOfTest Public Shared Sub Main() Dim strSource As String = "This is the string which we will perform the search on" Console.WriteLine("The search string is:{0}{1}{0}", Environment.NewLine, strSource) Dim strTarget As String = "" Dim found As Integer = 0 Dim totFinds As Integer = 0 Do Console.Write("Please enter a search value to look for in the above string (hit Enter to exit) ==> ") strTarget = Console.ReadLine() If strTarget <> "" Then Dim i As Integer For i = 0 To strSource.Length - 1 found = strSource.IndexOf(strTarget, i) If found > 0 Then totFinds += 1 i = found Else Exit For End If Next i Else Return End If Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}", Environment.NewLine, strTarget, totFinds) totFinds = 0 Loop While True End Sub 'Main End Class 'IndexOfTest
using System; public class IndexOfTest { public static void Main() { string strSource = "This is the string which we will perform the search on"; Console.WriteLine("The search string is:{0}\"{1}\"{0}", Environment.NewLine, strSource); string strTarget = ""; int found = 0; int totFinds = 0; do { Console.Write("Please enter a search value to look for in the above string (hit Enter to exit) ==> "); strTarget = Console.ReadLine(); if (strTarget != "") { for (int i = 0; i < strSource.Length; i++) { found = strSource.IndexOf(strTarget, i); if (found > 0) { totFinds++; i = found; } else break; } } else return; Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}", Environment.NewLine, strTarget, totFinds); totFinds = 0; } while ( true ); } }
using namespace System; int main() { String^ strSource = "This is the string which we will perform the search on"; Console::WriteLine( "The search string is: {0}\"{1}\" {0}", Environment::NewLine, strSource ); String^ strTarget = ""; int found = 0; int totFinds = 0; do { Console::Write( "Please enter a search value to look for in the above string (hit Enter to exit) ==> " ); strTarget = Console::ReadLine(); if ( !strTarget->Equals( "" ) ) { for ( int i = 0; i < strSource->Length; i++ ) { found = strSource->IndexOf( strTarget, i ); if ( found > 0 ) { totFinds++; i = found; } else break; } } else return 0; Console::WriteLine( "{0}The search parameter '{1}' was found {2} times. {0}", Environment::NewLine, strTarget, totFinds ); totFinds = 0; } while ( true ); }
.NET Framework
サポート対象: 4、3.5、3.0、2.0、1.1、1.0.NET Framework Client Profile
サポート対象: 4、3.5 SP1Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2
.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。