3

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?

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that works !! I may have to study more about splitting since i never used the overloads... I was converting with CChar because i didn't remember to use curly brackets in this case and Option Strict complained :p
2

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

2 Comments

Your solution also works, thank you very much, i didn't knew that ;)
Thanks for your answer, searched everywhere this is the only working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.