vb.net find line that contain in a string - vb.net

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 ?

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

Related

Visual Basic convert string to audio (WAV) file and play

I'm using a VB win form and need to play a wav from string but I don't know how to do it.
Dim URl As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(URl)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse
Dim responseStream As System.IO.Stream = response.GetResponseStream
Dim Reader As New System.IO.StreamReader(responseStream)
Dim data As String = Reader.ReadToEnd
Reader.Close()
Dim AudioData As String = Hex2String(data)
'here i wont play and save this data
You don't want to read the data as a string, since it's not a string. WAV data is binary data. It's meaningless as a string, and if you encode it as a string, you may lose some data in the process. So, if you aren't converting it to a string, you don't need the StreamReader at all. Just create a new file as a stream, and then copy all the bytes from the one stream to the other:
Dim url As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse()
Using responseStream As Stream = response.GetResponseStream()
Using fileStream As New New FileStream("MyFile.wav", FileMode.CreateNew)
responseStream.CopyTo(fileStream)
End Using
End Using

VB.NET compress decompress string and return in string format

I need a vb.net function for compressing/decompressing a string, and return the result as a string. I found these two functions:
//Compress
Dim mem As New IO.MemoryStream
Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
Dim sw As New IO.StreamWriter(gz)
sw.WriteLine("hello compression")
sw.Close()
//Decompress
Dim mem2 As New IO.MemoryStream(mem.ToArray)
gz = New System.IO.Compression.GZipStream(mem2, IO.Compression.CompressionMode.Decompress)
Dim sr As New IO.StreamReader(gz)
MsgBox(sr.ReadLine)
sr.Close()
But how do I make them return the compressed string as a string? thanks.
You would need to Base64 encode the compressed byte array to get a string representation.
Dim compressed As String = Convert.ToBase64String(mem.ToArray())
The decompressed string can just be read from the StreamReader.
Dim decompressed As String = sr.ReadLine()

Issue with StreamReader

I am writing code where I am trying to grab the HTML from a DNS report online (http://viewdns.info/dnsreport/?domain=google.com), but I am having some issues. The one line of the HTML file (Line 231) that I actually need is cutting itself off after around 680 characters. All of the lines after the important one are reading correctly, however. The code for grabbing the HTML is shown below, and I have tried it in two separate ways.
This is the first way I tried:
Public Function getWebResourceData(ByVal strURL As String) As String
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://viewdns.info/dnsreport/?" & TextBox1.Text)
return result
End Function
And this is the second:
Public Function getWebResourceData(ByVal strURL As String) As String
Dim rt As String = ""
Dim wRequest As WebRequest
Dim wResponse As WebResponse
Dim SR As StreamReader
wRequest = WebRequest.Create(strURL)
wResponse = wRequest.GetResponse
SR = New StreamReader(wResponse.GetResponseStream)
rt = SR.ReadToEnd
SR.Close()
return rt
End Function
Im really not sure what else could be wrong at this point. I have also tried saving the result to a text file to see if that was the issue, but that was incorrect as well. I have looked into the hex codes for the area where the string is stopping, but there isn't anything out of the ordinary. The split occurs between the back to back alligator brackets (shown as parentheses) here: (/tr)(tr)
But there are numerous sets of these tags throughout the HTML that there are no issues with.
Both of your functions don't return what they have read. I have tested the second one and it works correctly.
Sub Main
Dim ret = getWebResourceData("http://viewdns.info/dnsreport/?domain=google.com")
Console.WriteLine(ret.Length)
' Output = 21605
End Sub
Public Function getWebResourceData(ByVal strURL As String) As String
Dim rt As String = ""
Dim wRequest As WebRequest
Dim wResponse As WebResponse
Dim SR As StreamReader
wRequest = WebRequest.Create(strURL)
wResponse = wRequest.GetResponse
SR = New StreamReader(wResponse.GetResponseStream)
rt = SR.ReadToEnd
SR.Close()
return rt
End Function

Issue in splitting an array of strings

I'm using webrequests to retrieve data in a .txt file that's on my dropbox using this "format".
SomeStuff
AnotherStuff
StillAnother
And i'm using this code to retrieve each line and read it:
Private Sub DataCheck()
Dim datarequest As HttpWebRequest = CType(HttpWebRequest.Create("https://dl.dropboxusercontent.com.txt"), HttpWebRequest)
Dim dataresponse As HttpWebResponse = CType(datarequest.GetResponse(), HttpWebResponse)
Dim sr2 As System.IO.StreamReader = New System.IO.StreamReader(dataresponse.GetResponseStream())
Dim datastring() As String = sr2.ReadToEnd().Split(CChar(Environment.NewLine))
If datastring(datastring.Length - 1) <> String.Empty Then
For Each individualdata In datastring
MessageBox.Show(individualdata)
Console.WriteLine(individualdata)
Next
End If
End Sub
The problem is, the output is this:
It always adds a line break (equal to " " as i see as first character on each but the first line string) after the first line like:
http://img203.imageshack.us/img203/1296/gejb.png
Why this happens? I tried also replacing the Environment.Newline with nothing like this:
Dim newstring as String = individualdata.Replace(Environment.Newline, String.Empty)
But the result was the same... what's the problem here? I tried with multiple newline strings and consts like vbnewline, all had the same result, any ideas?
You are not splitting by NewLine since you are cutting off Environment.NewLine which is a string with CChar. You just have to use the overload of String.Split that takes a String() and a StringSplitOption:
So instead of
Dim text = sr2.ReadToEnd()
Dim datastring() As String = text.Split(CChar(Environment.NewLine))
this
Dim datastring() As String = text.Split({Environment.NewLine}, StringSplitOptions.None)
I suspect that your file contains a mix of NewLine+CarriageReturn (vbCrLf) and a simple NewLine (vbLf).
If this is the case then you could create an array of the possible separators
Dim seps(2) as Char
seps(0) = CChar(vbLf)
seps(1) = CChar(vbCr)
Dim datastring() As String = sr2.ReadToEnd().Split(seps, StringSplitOptions.RemoveEmptyEntries)
The StringSplitOptions.RemoveEmptyEntries is required because a vbCrLf creates an empty string between the two separators

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