たとえば、内部にいくつかの文字列を含むhtmlファイルがあります。
"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150000&cultuur=en-GB&continent=europa","http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150300&cultuur=en-GB&continent=europa","http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150600&cultuur=en-GB&continent=europa"
これは使用しているコードです:
コンストラクターで私がした:
f = File.ReadAllText(localFilename + "test.html");
retrivingText1();
private void retrivingText1()
{
string startTag = "http://www.niederschlagsradar.de/images.aspx";//"<Translation>";
string endTag = "continent=europa";//"</Translation>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
index = 0;
w = new StreamWriter(@"d:\retrivedText1.txt");
while (true)
{
index = f.IndexOf(startTag, index);
if (index == -1)
{
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
index = f.LastIndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start + endTagWidth).Trim(); //Trim the founded text so the start and ending spaces are removed.
w.WriteLine(g);
//break so you dont have an endless loop
break;
}
w.Close();
}
htmlファイルから抽出するには、htmlagilitypackまたは正規表現を使用する方がよいことを知っています。しかし、今回は indexof と substring を試してみたかったのです。
行でブレークポイントを使用する場合:
int start = index + startTagWidth;
開始 = 2950
インデックスの次の行 = -1