次の方法で共有


String.IndexOf メソッド (String)

指定した String がこのインスタンス内で最初に見つかった位置のインデックスをレポートします。

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

パラメータ

  • value
    シークする String

戻り値

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

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。

解説

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

このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。このインスタンスの最初の文字位置から最後の文字位置まで検索が実行されます。

使用例

IndexOf メソッドについては、次のコード例を参照してください。

 
Imports System

Public Class InsertTest
    
    Public Shared Sub Main()
        Dim animal1 As String = "fox"
        Dim animal2 As String = "dog"
        Dim strTarget As String = [String].Format("The {0} jumped over the {1}.", animal1, animal2)
        
        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget)
        
        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1)
        Dim adj1 As String = Console.ReadLine()
        
        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2)
        Dim adj2 As String = Console.ReadLine()
        
        adj1 = adj1.Trim() + " "
        adj2 = adj2.Trim() + " "
        
        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1)
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2)
        
        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget)
    End Sub 'Main
End Class 'InsertTest

[C#] 
using System;

public class InsertTest {
    public static void Main() {

        string animal1 = "fox";
        string animal2 = "dog";

        string strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2);

        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget);

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1);
        string adj1 = Console.ReadLine();

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2);    
        string adj2 = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);

        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget);
    }
}

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

using namespace System;
int main()
{
   String* animal1 = S"fox";
   String* animal2 = S"dog";

   String* strTarget = String::Format(S"The {0} jumped over the {1}.", animal1, animal2);

   Console::WriteLine(S"The original string is:{0}{1}{0}", Environment::NewLine, strTarget);

   Console::Write(S"Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1);
   String* adj1 = Console::ReadLine();

   Console::Write(S"Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2);    
   String* adj2 = Console::ReadLine();

   adj1 = String::Concat( adj1->Trim(), S" ");
   adj2 = String::Concat( adj2->Trim(), S" ");

   strTarget = strTarget->Insert(strTarget->IndexOf(animal1), adj1);
   strTarget = strTarget->Insert(strTarget->IndexOf(animal2), adj2);

   Console::WriteLine(S" {0}The final string is: {0} {1}", Environment::NewLine, strTarget);
}

[JScript] 
import System;

public class InsertTest {
    public static function Main() : void {

        var animal1 : String = "fox";
        var animal2 : String = "dog";

        var strTarget : String = String.Format("The {0} jumped over the {1}.", animal1, animal2);

        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget);

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1);
        var adj1 : String = Console.ReadLine();

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2);    
        var adj2 : String = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);

        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget);
    }
}
InsertTest.Main();

必要条件

プラットフォーム: 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