Public Class Tester
Public Shared Sub Main
Dim result As New System.Text.StringBuilder
result.AppendLine("A")
result.AppendLine("B")
result.AppendLine("C")
result.AppendLine("D")
result.Append("E")
Dim resultAsString As String = result.ToString()
Console.WriteLine(resultAsString)
resultAsString = InsertLine(resultAsString, 3, "(inserted)")
Console.WriteLine(resultAsString)
End Sub
Public Shared Function InsertLine(ByVal source As String, _
ByVal lineNum As Integer, _
ByVal lineToInsert As String) As String
Dim lineSet() As String
Dim atLine As Integer
lineSet = Split(source, vbNewLine)
atLine = lineNum
If (atLine < 0) Then atLine = 0
If (atLine >= lineSet.Length) Then
lineSet(lineSet.Length - 1) &= vbNewLine & lineToInsert
Else
lineSet(atLine) = _
lineToInsert & vbNewLine & lineSet(atLine)
End If
Return Join(lineSet, vbNewLine)
End Function
End Class
A
B
C
D
E
A
B
C
(inserted)
D
E