在特定标记后插入文本

时间:2013-05-21 11:50:29

标签: vb.net text insert tags

我有一个由不同标签组成的文本文件。我能够使用以下内容查明文档中是否存在特定标记...

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim text As String = IO.File.ReadAllText("C:\Example.xtp")
    Dim index As Integer = text.IndexOf("<Tools>")
    If index >= 0 Then
        ' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"

    End If
End Sub
End Class

但是我想在/如果找到

之后在此标签后面输入额外的文字

我正在使用VB.net

1 个答案:

答案 0 :(得分:0)

尝试:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim text As String = IO.File.ReadAllText("C:\Example.xtp")
    Dim index As Integer = text.IndexOf("<Tools>")
    Dim countChars as integer
    countChars="<Tools>".Length
    If index >= 0 Then
       ' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"
        text = text.Insert(index + countChars, "TEST_HELLO")
    End If
End Sub

修改的 如果要将最终文本写入/文件,有很多方法。我只是建议一个,但你需要搜索和阅读并找到适合你的东西:

Dim Writer As System.IO.StreamWriter
Writer = New System.IO.StreamWriter("C:\Textfile.txt") '<-- Where to write to
Writer.Write(text)
Writer.Close()