String.Insert メソッドとは? わかりやすく解説

String.Insert メソッド

このインスタンス内の指定したインデックス位置に、指定した String インスタンス挿入します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (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
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);
    }
}
using namespace System;
int 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 = String::Concat( adj1->Trim(), " " );
   adj2 = String::Concat( 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 );
}

import System.*;

public class InsertTest
{
    public static void main(String[]
 args)
    {
        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.get_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.get_NewLine(), strTarget);
    } //main
} //InsertTest
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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からString.Insert メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からString.Insert メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からString.Insert メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Insert メソッド」の関連用語

String.Insert メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Insert メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS