我正在尝试获取包含特定单词的行之前的行的列表。这是我的脚本:从文件中读取特定行
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();
}
你多少行会希望保持到该字后发现 –
外貌就像一个家庭作业问题,其中提示是行列表已被声明但未被使用,所以你只需要弄清楚如何添加一些东西到列表中,然后引用先前的事件。 – PeterJ
如果行包含寻找文件结束和退出循环,否则写入行 – 2012-12-30 06:53:55