DownloadString skips newline characters

自闭症网瘾萝莉.ら 提交于 2021-02-09 12:44:31

问题


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?


回答1:


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



回答2:


There ARE newline characters in the file. Check it in a hex editor. They are Unix line-endings, \n (0x0A), not Windows line-endings, \r\n (0x0D 0x0A). You can feed your string to a StringReader and then read it line by line, and then write it line by line to somewhere else, to normalize line endings, or you can just do a replace operation.

DownloadString does NOT alter the downloaded contents, the only problem could be mismatched encodings.



来源:https://stackoverflow.com/questions/26928158/downloadstring-skips-newline-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!