user1119361
user1119361

Reputation: 23

Can't split string with space character using Split(" "c) vb.net

I'm trying to split a Yahoo historical stock price csv file, downloaded into a string, by what looks like a space character. I want a new row for each split. The split function works for other characters I see in the string. I suspect the characters may be a non breaking space character but I've been unable to split on them.

This is the test csv file that is downloaded into the string: http://ichart.finance.yahoo.com/table.csv?s=AAPL&c=2011

I'm trying to split the string like this:

Dim rows As String() = data.Split(" "c)

There is a real space character in the header part of the string that this breaks on, but not the white space characters in the stock data I want to split. If this is a non breaking space, how do I split on it? How can I tell what this white space character is?

A Sample of the string looks like this:

"Date,Open,High,Low,Close,Volume,Adj Close 2011-12-27,69.24,72.18,69.01,71.55,1491000,71.55 2011-12-23,67.49,69.25,67.25,69.08,880300,69.08"

I'm trying to split at the space before the stock dates, "2011-12-23", for example.

This is my function:

Public Shared Function DownloadData(ByVal ticker As String, ByVal yearToStartFrom As Integer) As List(Of HistoricalStock)

Dim retval As New List(Of HistoricalStock)()

Using web As New WebClient()
Dim data As String = web.DownloadString(String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&c={1}", ticker, yearToStartFrom))

Dim rows As String() = data.Split(" "c)

Return retval
End Using
End Function

Upvotes: 2

Views: 6011

Answers (2)

Meta-Knight
Meta-Knight

Reputation: 17845

Those "spaces" that you talk about aren't really spaces, they are line returns. You probably opened it with NotePad, where it's displayed as a space, because it tries to open it with the wrong encoding, I suppose.

Open it with WordPad or Excel and you'll see the line returns. You will need to split on vbLf for it to work:

Dim rows As String() = data.Split(vbLf)

Upvotes: 2

UnhandledExcepSean
UnhandledExcepSean

Reputation: 12804

Those are not spaces. They are line feeds. This will correct it.

Dim rows As String() = data.Split(vbLf)

Upvotes: 1

Related Questions