Reputation: 41
I have this code so far. It looks through a text document and displays lines with the word word
in them. I want to make it skip that line and display the next one in the text document, how do I do that?
e.g. it looks thought the text document and finds a line with the word "word" in it and then displays the line that comes after it no other line
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();
Upvotes: 0
Views: 4465
Reputation: 2984
Something like this will show all the lines except empty lines and lines with the word "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();
Upvotes: 1
Reputation: 337570
If you're trying to write the line following an occurence of word
in a line, try this:
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();
Upvotes: 1
Reputation: 5515
This should display all lines after those that contain "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();
Upvotes: 0