在文本文件中搜索单词并继续从那里读取vb.net

时间:2012-09-29 11:24:25

标签: vb.net string text text-files vb.net-2010

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

代码段如下

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中。 我怎样才能回写文件,但现在从单词“to”开始 例如,outpout文本文件将是

进行测试 班级

我希望自己清楚明白

1 个答案:

答案 0 :(得分: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()