'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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve