C# 在第行中搜索关键字并打印该行和下一行
我使用以下方法解析文本文件并编写包含特定关键字的行:C# 在第行中搜索关键字并打印该行和下一行,c#,C#,我使用以下方法解析文本文件并编写包含特定关键字的行: using (StreamReader sr = new StreamReader("C:/Users/Downloads/apple.txt")) { string appleLine; bool lastLine = false; // currentLine will be null when the StreamReader reaches the
using (StreamReader sr = new StreamReader("C:/Users/Downloads/apple.txt"))
{
string appleLine;
bool lastLine = false;
// currentLine will be null when the StreamReader reaches the end of file
while ((appleLine = sr.ReadLine()) != null)
{
// Search, case insensitive, if the currentLine contains the searched keyword
if (appleLine.IndexOf("Apple", StringComparison.CurrentCultureIgnoreCase) >= 0 || lastLine)
{
Console.WriteLine(appleLine);
Console.WriteLine();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Downloads\parsed.txt", true))
{
file.WriteLine(appleLine);
file.WriteLine();
}
lastLine = true;
}
if (lastLine)
{
lastLine = false;
}
}
在apple.txt中,我有如下内容:
---Line1苹果MacBook Pro---
---第二行www.newegg.com---
但这不会打印出带有URL的行(第2行)。apple.txt文件可能有大约200行
非常感谢你的帮助 您可以尝试使用布尔值指示是否已打印“currentline”,然后在打印下一行后中断循环。这样不仅会打印当前行,还会打印下一行,然后退出while循环
using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
string currentLine;
**bool lastline = false;**
// currentLine will be null when the StreamReader reaches the end of file
while((currentLine = sr.ReadLine()) != null)
{
// Search, case insensitive, if the currentLine contains the searched keyword
if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0 **|| lastline**)
{
Console.WriteLine(currentLine);
lastline = true;
}
**//Optional, could also say lastline = false inside if, in case you want to keep looping after printing**
**if(lastline){
break;
}**
}
}
如果没有更精确的问题陈述,就不可能确切地知道您想要什么样的输出。但到目前为止,类似以下内容将满足目标:
using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
bool printWindow = false;
Queue<string> window = new Queue<string>();
string currentLine;
// currentLine will be null when the StreamReader reaches the end of file
while((currentLine = sr.ReadLine()) != null)
{
bool nextPrintWindow = false
// Search, case insensitive, if the currentLine contains the searched keyword
if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
nextPrintWindow = true;
}
if (window.Count >= 3)
{
window.Dequeue();
}
window.Enqueue(currentLine);
if (printWindow)
{
Console.WriteLine(string.Join(Environment.NewLine, window));
}
printWindow = nextPrintWindow;
}
}
使用(StreamReader sr=新的StreamReader(“c:/temp/ESMDLOG.csv”))
{
bool printWindow=false;
队列窗口=新队列();
串电流线;
//当StreamReader到达文件末尾时,currentLine将为空
而((currentLine=sr.ReadLine())!=null)
{
bool nextPrintWindow=false
//搜索,不区分大小写,如果currentLine包含搜索的关键字
if(currentLine.IndexOf(“I/RPTGEN”,StringComparison.CurrentCultureIgnoreCase)>=0)
{
nextPrintWindow=true;
}
如果(window.Count>=3)
{
window.Dequeue();
}
排队(currentLine);
如果(打印窗口)
{
Console.WriteLine(string.Join(Environment.NewLine,window));
}
printWindow=nextPrintWindow;
}
}
(警告:以上代码仅适用于浏览器。请原谅打字错误和其他疏忽。)
上述方法通过将当前行排队,并在集合达到三个元素的计数时将最早的行排队,来维护窗口
集合,即文件中文本的三行“窗口”
同时,它维护一个标志,指示在读取下一行后是否打印窗口的内容。如果此标志设置为true
,则它将窗口中的行与换行符连接起来,并将整个内容打印出来
这样,如果当前行符合匹配条件,则当前行以及上一行和下一行都将打印出来
请注意,上面将为每个匹配打印三行窗口。例如,如果连续行满足匹配条件,则可以多次打印行(作为不同三个行组的一部分)。如果连续行满足匹配条件,另一种实现将允许窗口增长超过三个元素,仅在读取不满足匹配条件的行时打印(并重新启动)窗口
另一种选择是在打印窗口之前需要两条不匹配的行,即如果您希望确保即使是不匹配的行也只打印一次
我把这些变化留给读者作为练习。它们与上面没有太大的不同,只是在维护窗口和管理封装在标志变量中的隐含状态机方面做了一些小的更改。尝试使用一个布尔值,显示您找到了要打印的内容,并且还需要打印下一行。例如:
class Program
{
static void Main(string[] args)
{
bool nextLineToPrint = false;
using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
string currentLine;
// currentLine will be null when the StreamReader reaches the end of file
while ((currentLine = sr.ReadLine()) != null)
{
if (nextLineToPrint)
{
Console.WriteLine(currentLine);
nextLineToPrint = false;
}
// Search, case insensitive, if the currentLine contains the searched keyword
if (currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Console.WriteLine(currentLine);
nextLineToPrint = true;
}
}
}
Console.ReadLine();
}
}
这就是我想做的。并且它应该在打印当前行和最后一行后循环。不管怎么说,我没有让它起作用。你完全改变了问题。您不再要求在找到的行之前、之后和包括之前打印该行。如果你真的想问一个不同的问题,请将其作为新问题发布,而不是更改现有问题。如果你不想问一个新问题,那么不要改变这个问题。无论哪种方式,请回滚编辑,以便保留原始问题。请注意,更改问题会使现有答案无效,并浪费每个人的时间。