【发布时间】:2012-12-30 06:49:41
【问题描述】:
我正在尝试获取包含特定单词的行之前的行列表。这是我的脚本:
private static void Main(string[] args)
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("E:\\overview2.srt");
List<string> lines = new List<string>();
while ((line = file.ReadLine()) != null)
{
if (line.Contains("medication"))
{
int x = counter - 1;
Console.WriteLine(x); // this will write the line number not its contents
}
counter++;
}
file.Close();
}
【问题讨论】:
-
找到单词后你要保留多少行
-
看起来像一个家庭作业问题,提示行列表已声明但未使用,因此您只需要弄清楚如何向列表中添加一些内容,然后引用较早出现的内容。
-
如果行包含寻找文件结尾并退出循环,否则写行