Question Search for a string and return the whole line

theodoros2008

Member
Joined
Jun 16, 2008
Messages
5
Programming Experience
1-3
Hi everyone.

I have a textfile with a lot data.I want to search the text for a particular string but i want to return the whole line,the line that contains the string.I got so far

VB.NET:
 Dim svalue As String
        svalue = Me.DataGridView1.SelectedCells(0).Value

        If FindStringInFile(Application.StartupPath + "\oui.txt", svalue.Substring(0, 6)) Then
            MessageBox.Show("found " + svalue.Substring(0, 6))
        Else
            MessageBox.Show("not found")
        End If

VB.NET:
Public Function FindStringInFile(ByVal Filename As String, ByVal SearchString As String) As Boolean
        Dim Reader As System.IO.StreamReader
        Reader = New IO.StreamReader(Filename)
        Dim stringReader As String
        Try
            While Reader.Peek <> -1
                stringReader = Reader.ReadLine()
                If InStr(stringReader, SearchString) > 0 Then Return True
            End While
            Reader.Close()
            Return False
        Catch ex As Exception
            MessageBox.Show("Exception: " & ex.Message)
            Return False
        End Try
    End Function

Any ideas?
Thanks in advance.
 
At the point that you determine (using InStr.. that's old by the way, you should be using line.Contains("search text")) that the line DOES indeed contain the search text..

WHY do you return a boolean?

Why dont you just return THE LINE? (it is, after all, by your definition, what you are looking for)
 
Back
Top