Hey!
I have a program that searches for a word in a .txt file. When it finds the word I want to print the following 5 lines under that word. So for example:
I search for "juice", and my label grabs this out of the textfile:
juice
apple
grape
orange
milk

Is there anyway to this in VB.NET?

Recommended Answers

All 5 Replies

Hi,

Sure read through the file, for each line use the instr function to see if the word exists in that line.

I think you misunderstood. I know how to find the one line, but I need to somehow print the following 5 lines after the line that word is in. That's where my problem is.

Actually I explained it bad.
Let me try again:
I have a code that searches for a word, it finds that word and views a bit of info on the same line as that word. What I need is to also be able to view the next 5 lines as well.

Hi
But once you've found the target line, then put a second loop in your code to read in the next five.

Dim sr As New System.IO.StreamReader("MyFile")
        Dim i As Integer
        Dim Line As String
        While sr.EndOfStream = False
            Line = sr.ReadLine
            If InStr(Line, "Juice") <> 0 Then
                'do what ever with the line
                For i = 0 To 4
                    Line = sr.ReadLine
                    'print
                Next
            End If
        End While

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.