1

If I can find line that contains word in a file

File.ReadAllLines(html).FirstOrDefault(Function(x) x.Contains("something"))

How can I find all lines that contains in a string for example I made an webresponse

    Dim rt As String = "http://www.somesaite.com"
    Dim wRequest As WebRequest
    Dim WResponse As WebResponse
    Dim SR As StreamReader
    wRequest = FtpWebRequest.Create(rt)
    WResponse = wRequest.GetResponse
    SR = New StreamReader(WResponse.GetResponseStream)
    rt = SR.ReadToEnd

How to find lines that contains in rt ?

noidea
  • 184
  • 5
  • 22
Snoopy Ohoo
  • 109
  • 1
  • 11
  • 3
    Either read line by line by using a loop and `SR.ReadLine()` or split the whole text into an array like this: `Dim arr() As String = SR.ReadToEnd().Split(Environment.NewLine)`. – Visual Vincent Mar 15 '16 at 15:48
  • @VisualVincent you mean Split the whole html code line by line to arry then use `For Each Str As String In arr If Str.Contains("something")` ????? – Snoopy Ohoo Mar 15 '16 at 16:26
  • 1
    No, split it into an array and call `.FirstOrDefault(Function(x) x.Contains("something"))` on that array. `File.ReadAllLines()` returns an array of string. – Visual Vincent Mar 15 '16 at 17:10
  • @VisualVincent can you pleas write it as an answer for me i only mange to get one line and i want more than one – Snoopy Ohoo Mar 15 '16 at 18:41
  • 1
    Yep, give me a few minutes – Visual Vincent Mar 15 '16 at 18:41
  • A little change... Could you first show me what you get and how you get it? Becase I am uncertain what you get from the response. – Visual Vincent Mar 15 '16 at 19:04
  • @VisualVincent `Dim html As String = SR.ReadToEnd` then ` Dim htmlarr() As String = html.Split(Environment.NewLine)` and `Dim find As String = htmlarr.FirstOrDefault(Function(x) x.Contains("somethingl"))` – Snoopy Ohoo Mar 15 '16 at 20:01
  • 1
    My answer works for me... – Visual Vincent Mar 15 '16 at 20:07

1 Answers1

0

You could read all the text that the StreamReader gives you, and then you could split that by the Environment.NewLine character(s). Then you should just be able to use the lambda expression you first mentioned (as the File.ReadAllLines() method returns an array of strings).

Dim FoundLine As String = SR.ReadToEnd().Split(Environment.NewLine).FirstOrDefault(Function(x) x.Contains("something"))    
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75