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)

'Declaration
Public Function Insert ( _
	startIndex As Integer, _
	value As String _
) As String

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 Nothing.

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.


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Initialize random number generator to define adjectives to use.
      Dim rnd As New Random()

      Dim animal1 As String = "fox"
      Dim animal2 As String = "dog"
      Dim animal1Adjectives() As String = { "quick", "fast", "speedy", "energetic" }
      Dim animal2Adjectives() As String = { "lazy", "sleeping", "slow-moving", "slothful" }
      Dim strTarget As String = String.Format("The {0} jumped over the {1}.", animal1, animal2)
      outputBlock.Text &= String.Format("The original string is:{0}{1}{0}", vbCrLf, strTarget)

      ' Generate a random number to extract an adjective from the array
      ' to describe each animal.
      Dim animal1AdjectivePosition As Integer = rnd.Next(animal1Adjectives.GetLowerBound(0), _ 
                                              animal1Adjectives.GetUpperBound(0) + 1)
      Dim animal2AdjectivePosition As Integer = rnd.Next(animal1Adjectives.GetLowerBound(0), _ 
                                              animal1Adjectives.GetUpperBound(0) + 1)                                              
      Dim animal1Adjective As String = animal1Adjectives(animal1AdjectivePosition) + " "
      Dim animal2Adjective As String = animal2Adjectives(animal2AdjectivePosition) + " "

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

      outputBlock.Text &= String.Format("{0}The final string is:{0}{1}{0}", vbCrLf, strTarget)
   End Sub 
End Class 
' 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.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft