有一个只包含文字的网址。
格式为
firstWord~:::~secondWord
目标是寻找secondWord
,然后Console.WriteLine
firstWord
。
如果我不得不猜测,我会说使用WebClient
将文本文件下载到string
,然后使用regex
查看它以找到它。
问题是这个文件大概是1千兆字节,我不知道最快的方法是什么。
有什么想法吗?谢谢!
答案 0 :(得分:0)
以下是我能想到的最快的方法。这并不是说你不能在这里和那里刮几个滴答...很高兴其他反馈,看看我们是否可以简化这个。
// WebClient is very bulky with a lot of stuff we don't need.
// By dealing with the request, response and stream ourself we can get the string a bit faster.
WebRequest request = WebRequest.Create("http://www.UrlToDownloadStringFrom.com");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
// the result should return "firstWord~:::~secondWord" as expected.
string result = streamReader.ReadToEnd();
// split the string apart whenever the string ~:::~ appears within it.
string[] resultSplit = result.Split(new string[] { "~:::~" }, StringSplitOptions.None);
// resultSplit[0] is firstWord, resultSplit[1] is second word
string secondWord = resultSplit[1];