0

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?

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by fenhopi
0

Hi,

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

0

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.

0

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.

0

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.