How to VB.NET String.Insert()

The Insert() function in the String Class is a powerful method that allows you to insert a string at a specified index within an existing string instance. This function provides a flexible way to modify and manipulate strings by adding additional content at a desired position.

Insert() function

By using the Insert() function, you can specify the index within the string where the insertion should occur and provide the string that you want to insert. The method then seamlessly integrates the specified string into the original string at the designated position, effectively expanding the string with the new content.

This functionality proves particularly useful when you need to dynamically modify strings by adding or injecting additional text or characters at specific locations. Whether you are building complex strings, manipulating user input, or transforming data, the Insert() function empowers you to customize and shape your strings precisely as needed.

System.String.Insert(Integer ind, String str) as String
  1. ind - The index of the specified string to be inserted.
  2. str - The string to be inserted.
  1. String - The result string.
  1. System.ArgumentOutOfRangeException: startIndex is negative or greater than the length of this instance
  2. System.ArgumentNullException : If the argument is null.

For ex:

"This is Test".Insert(8,"Insert ") returns "This is Insert Test"

With the Insert() function in the String Class, you can efficiently and conveniently modify strings by incorporating additional content at specific indices. This method enhances the versatility and flexibility of string manipulation in VB.NET, allowing you to create dynamic and adaptable string structures in your applications.

Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim str As String = "This is VB.NET Test" Dim insStr As String = "Insert " Dim strRes As String = str.Insert(15, insStr) MsgBox(strRes) End Sub End Class


The Insert() function in the String Class is a valuable tool for string manipulation, enabling you to seamlessly insert a string at a specified index within an existing string instance. This method empowers developers to tailor and modify strings dynamically, opening up possibilities for customizing and transforming string data in their VB.NET applications.