Alexander Sukhanov Alexander Sukhanov - 28 days ago 21
C# Question

DownloadString skips newline characters

I want to import text data from Google Finance, and I use this http address as a parameter to

DownloadString
http://www.google.com/finance/getprices?i=1200&p=1d&f=d,o,h,l,c,v&df=cpct&q=AAPL
. However, the resulting string misses any newline characters, so it is really difficult to parse. Any ideas?

Answer

The line ends returned from the stream are \n opposed to the default Windows line ends \r\n (which is represented in Environment.NewLine on Windows).

Try to split on all of the possible combinations of \r and \n:

WebClient wc = new WebClient();
string s = wc.DownloadString("http://www.google.com/finance/getprices?i=1200&p=1d&f=d,o,h,l,c,v&df=cpct&q=AAPL");

string[] lines = s.Split(new string[] { Environment.NewLine, "\n", "\"r" }, StringSplitOptions.None);
Comments