C# 查找一行并读取/显示文本文档C中的下一行#
到目前为止,我有这个代码。它浏览文本文档并显示其中包含单词C# 查找一行并读取/显示文本文档C中的下一行#,c#,streamreader,C#,Streamreader,到目前为止,我有这个代码。它浏览文本文档并显示其中包含单词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 ((l
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();
这将显示包含
“word”
的行之后的所有行
如果您试图在一行中出现
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();
类似的内容将显示除空行和带有单词“word”的行之外的所有行
不清楚和不完整。使用
计数器
会发生什么情况?除了包含单词
字符串的行之外,您只想写其他行?你的问题不清楚,我想..即使是在主题上也有一些琐碎的逻辑问题吗?你的意思是你从某个地方复制了代码,现在你不知道如何反转if子句?例如,它在文本文档中找到一行,其中包含单词“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();
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();