到目前为止我有这个代码。它通过文本文档查看并显示其中包含单词word
的行。我想让它跳过那一行并在文本文档中显示下一行,我该怎么做?
e.g。它看起来像文本文档,并在其中找到一个单词“word”,然后显示其后没有其他行的行
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("word"))
{
Console.WriteLine(line);
}
}
file.Close();
答案 0 :(得分:1)
如果您尝试在一行中出现word
后写一行,请尝试以下操作:
int counter = 0;
bool writeNextLine = false;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
while ((line = file.ReadLine()) != null)
{
if (writeNextLine)
{
Console.WriteLine(line);
}
writeNextLine = line.Contains("word");
counter++;
}
file.Close();
答案 1 :(得分:0)
这应显示包含"word"
。
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("word"))
{
if ((line = file.ReadLine()) != null)
Console.WriteLine(line);
}
counter++;
}
file.Close();
答案 2 :(得分:0)
这样的内容将显示除空行和除“word”
之外的所有行using (var rdr = new StreamReader(@"C:\Users\Gebruiker\Desktop\text.txt"))
{
while (!(rdr.EndOfStream))
{
var line = rdr.ReadLine();
if (!(line.Contains("word")) && (line != String.Empty))
{
Console.WriteLine(line);
}
}
}
Console.ReadKey();