1

正如标题所说,我想在文本文件中搜索特定单词,然后从该点开始阅读文本。

代码片段如下

Dim path As String = "c:\temp\test.txt"
        Dim readall As String
        Dim i As Integer


            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("Hello")
            sw.WriteLine("i am here")
            sw.WriteLine("to test")
            sw.WriteLine("the class")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)
            readall = sr.ReadToEnd
            sr.Close()

所以现在我已经阅读了文件并将其存储到字符串 readall 中。我怎样才能写回文件,但现在从“到”这个词开始例如输出文本文件将是

测试类

我希望我说清楚了

4

1 回答 1

1

搜索readall第一次出现的“to”并将其余的readall以“to”开头的内容存储到变量中stringToWrite

Dim stringToWrite As String
stringToWrite = readall.Substring(IndexOf("to"))

然后写入stringToWrite文件:

Dim sw As StreamWriter = New StreamWriter(path)
sw.write(stringToWrite)
sw.Close()
于 2012-09-29T11:31:02.030 回答