Pantheo
Pantheo

Reputation: 129

Search for word in text file and continue reading from there vb.net

As the title say i would like to search a text file for a specific word and then start reading the text from that point forth.

A code snippet is the following

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()

So right now i have read the file and stored it to string readall. How can i write back to the file but now starting from the word "to" For instance the outpout text file would be

to test the class

I hope i made myself clear

Upvotes: 1

Views: 4782

Answers (1)

poplitea
poplitea

Reputation: 3737

Search readall for the first occurrence of "to" and store the rest of readall, starting with "to", to the variable stringToWrite:

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

Then write stringToWrite to file:

Dim sw As StreamWriter = New StreamWriter(path)
sw.write(stringToWrite)
sw.Close()

Upvotes: 1

Related Questions