String.Insert メソッド
このインスタンス内の指定したインデックス位置に、指定した String インスタンスを挿入します。
Public Function Insert( _
ByVal startIndex As Integer, _ ByVal value As String _) As String
[C#]
public string Insert(intstartIndex,stringvalue);
[C++]
public: String* Insert(intstartIndex,String* value);
[JScript]
public function Insert(
startIndex : int,value : String) : String;
パラメータ
- startIndex
挿入先インデックス位置。 - value
挿入する String 。
戻り値
このインスタンスと等価で、 startIndex の位置に value が挿入された新しい String 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | value が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | startIndex が負か、またはこのインスタンスの長さより大きい値です。 |
解説
startIndex とこのインスタンスの長さが等しい場合は、このインスタンスの末尾に value が追加されます。
たとえば、 "abc".Insert(2, "XYZ")
の戻り値は "abXYZc" です。
使用例
Insert メソッドの簡単な例を次のコンソール アプリケーションで示します。
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 名前空間 | Int32 | Concat | CopyTo | Insert | Join | Remove | Replace | Split | Substring | Trim