CorString: Insert

Insert

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



 Public Function Insert(
	  ByRef s As String,
	  ByVal StartIndex As Long,
	  ByRef Value As String ) As String

Parameters

s
[ByRef] String. The string to insert to.
StartIndex
[ByVal] Long. The zero-base index position of the insertion.
Value
[ByRef] String. The string to insert.

Return Values

String -  A new string that is equivalent to this instance, but with Value inserted at position StartIndex.

Remarks

If StartIndex is equal to the length of s, value is appended to the end of s.
Note
This method does not modify the value of s. Instead, it returns a new string in which Value is inserted into s.

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

Exceptions

ExceptionCondition
ArgumentOutOfRangeException StartIndex is negative or greater than the length of s.

Examples

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

Public Sub Main()
    Const Animal1   As String = "fox"
    Const Animal2   As String = "dog"
    Const Adj1      As String = "bold "
    Const Adj2      As String = "lazy "
    Dim Target      As String
    
    Target = CorString.Format("The {0} jumped over the {1}.", Animal1, Animal2)
    
    Debug.Print CorString.Format("The original string is: {0}{1}", vbCrLf, Target)
    
    Target = CorString.Insert(Target, InStr(Target, Animal1) - 1, Adj1)
    Target = CorString.Insert(Target, InStr(Target, Animal2) - 1, Adj2)
    
    Debug.Print CorString.Format("{0}The final string is: {0}{1}", vbCrLf, Target)
End Sub

' This example produces the following output.
'
'    The original string is:
'    The fox jumped over the dog.
'
'    The final string is:
'    The bold fox jumped over the lazy dog.

See Also

Project CorLib Overview

Class CorString Overview

Remove