String.Insert Method (Int32, String)

 

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)

member Insert : 
        startIndex:int *
        value:string -> string

Parameters

startIndex
Type: System.Int32

The zero-based 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.

Exception Condition
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.

System_CAPS_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 inserts a space character in the fourth character position (the character at index 3) of a string.

No code example is currently available or this language may not be supported.

The following console application prompts the users to enter one or more adjectives to describe two animals. It then calls the Insert method to insert the text entered by the user into a string.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: