Export (0) Print
Expand All
2 out of 11 rated this helpful - Rate this topic

String.Insert Method

Inserts a specified instance of String at a specified index position in this instance.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string Insert (
	int startIndex,
	string value
)
public String Insert (
	int startIndex, 
	String value
)
public function Insert (
	startIndex : int, 
	value : String
) : String

Parameters

startIndex

The index position of the insertion.

value

The String to insert.

Return Value

A new String equivalent to this instance but with value inserted at position startIndex.
Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startIndex is negative or greater than the length of this instance.

If startIndex is equal to the length of this instance, value is appended to the end of this instance.

For example, the return value of "abc".Insert(2, "XYZ") is "abXYZc".

The following console application provides a simple demonstration of the Insert method.

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);
    }
}

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();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.