-1

我有一个由不同标签组成的文本文件。我可以使用以下命令找出文档中是否存在特定标签...

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

4

1 回答 1

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()
于 2013-05-21T12:00:37.543 回答