Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Insert Method

Returns a new string in which a specified string is inserted at a specified index position in this instance.

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

[SecuritySafeCriticalAttribute]
public string Insert(
	int startIndex,
	string value
)

Parameters

startIndex
Type: System.Int32
The index position of the insertion.
value
Type: System.String
The string to insert.

Return Value

Type: System.String
A new string that is equivalent to this instance, but with value inserted at position startIndex.

ExceptionCondition
ArgumentNullException

value is null.

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.

NoteNote:

This method does not modify the value of the current instance. Instead, it returns a new string in which value is inserted into the current instance.

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

The following example uses the Insert method to insert two randomly selected adjectives in front of two nouns in a string.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Initialize random number generator to define adjectives to use.
      Random rnd = new Random();

      string animal1 = "fox";
      string animal2 = "dog";
      string[] animal1Adjectives = { "quick", "fast", "speedy", "energetic" };
      string[] animal2Adjectives = { "lazy", "sleeping", "slow-moving", "slothful" };
      string strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2);
      outputBlock.Text += String.Format("The original string is:\n{0}\n", strTarget);

      // Generate a random number to extract an adjective from the array
      // to describe each animal.
      int animal1AdjectivePosition = rnd.Next(animal1Adjectives.GetLowerBound(0),  
                                              animal1Adjectives.GetUpperBound(0) + 1);
      int animal2AdjectivePosition = rnd.Next(animal1Adjectives.GetLowerBound(0),  
                                              animal1Adjectives.GetUpperBound(0) + 1);                                             
      string animal1Adjective = animal1Adjectives[animal1AdjectivePosition] + " ";
      string animal2Adjective = animal2Adjectives[animal2AdjectivePosition] + " ";

      strTarget = strTarget.Insert(strTarget.IndexOf(animal1), animal1Adjective);
      strTarget = strTarget.Insert(strTarget.IndexOf(animal2), animal2Adjective);

      outputBlock.Text += String.Format("\nThe final string is:\n{0}\n", strTarget);
   }
}
// The output from the example may appear as follows:
//       The original string is:
//       The fox jumped over the dog.
//
//       The final string is:
//       The quick fox jumped over the slothful dog.


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2014 Microsoft