c#在txt文件中搜索字符串
我想在txt文件中找到一个字符串,如果字符串比较,它应该继续读取行,直到另一个字符串被用作参数 例如:c#在txt文件中搜索字符串,c#,string,file-io,C#,String,File Io,我想在txt文件中找到一个字符串,如果字符串比较,它应该继续读取行,直到另一个字符串被用作参数 例如: CustomerEN//搜索此字符串 ... 一些包含客户详细信息的文本 id“123456” 用户名“rootuser” ... CustomerCh//获取此字符串之前的文本 我需要这些细节来与他们合作 我使用linq搜索“CustomerEN”,如下所示: File.ReadLines(pathToTextFile).Any(line => line.Contains("
CustomerEN//搜索此字符串
...
一些包含客户详细信息的文本
id“123456”
用户名“rootuser”
...
CustomerCh//获取此字符串之前的文本
我需要这些细节来与他们合作
我使用linq搜索“CustomerEN”,如下所示:
File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN"))
var importantLines =
File.ReadLines(pathToTextFile)
.SkipWhile(line => !line.Contains("CustomerEN"))
.TakeWhile(line => !line.Contains("CustomerCh"));
但现在我一直在阅读行(数据)直到“CustomerCh”来提取细节。如果只需要第一个字符串,可以使用simple for循环
var lines = File.ReadAllLines(pathToTextFile);
var firstFound = false;
for(int index = 0; index < lines.Count; index++)
{
if(!firstFound && lines[index].Contains("CustomerEN"))
{
firstFound = true;
}
if(firstFound && lines[index].Contains("CustomerCh"))
{
//do, what you want, and exit the loop
// return lines[index];
}
}
var lines=File.ReadAllLines(pathToTextFile);
var firstFound=false;
对于(int index=0;index
对于LINQ,您可以使用SkipWhile
/TakeWhile
方法,如下所示:
File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN"))
var importantLines =
File.ReadLines(pathToTextFile)
.SkipWhile(line => !line.Contains("CustomerEN"))
.TakeWhile(line => !line.Contains("CustomerCh"));
如果这两行在文件中只出现一次,则可以使用
File.ReadLines(pathToTextFile)
.SkipWhile(line => !line.Contains("CustomerEN"))
.Skip(1) // optional
.TakeWhile(line => !line.Contains("CustomerCh"));
如果您可以在一个文件中多次出现,您最好使用常规的foreach
循环读取行,跟踪您当前是在客户内部还是外部,等等:
List<List<string>> groups = new List<List<string>>();
List<string> current = null;
foreach (var line in File.ReadAllLines(pathToFile))
{
if (line.Contains("CustomerEN") && current == null)
current = new List<string>();
else if (line.Contains("CustomerCh") && current != null)
{
groups.Add(current);
current = null;
}
if (current != null)
current.Add(line);
}
列表组=新列表();
列表当前=空;
foreach(File.ReadAllLines(pathToFile)中的var行)
{
if(line.Contains(“CustomerEN”)&¤t==null)
当前=新列表();
else if(line.Contains(“CustomerCh”)&¤t!=null)
{
组。添加(当前);
电流=零;
}
如果(当前!=null)
当前。添加(行);
}
您必须在时使用,因为foreach
不知道索引。下面是一个示例代码
int counter = 0;
string line;
Console.Write("Input your search text: ");
var text = Console.ReadLine();
System.IO.StreamReader file =
new System.IO.StreamReader("SampleInput1.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
break;
}
counter++;
}
Console.WriteLine("Line number: {0}", counter);
file.Close();
Console.ReadLine();
我使用了Rawling在这里发布的方法,在同一个文件中查找多行,直到最后。这就是我的工作原理:
foreach (var line in File.ReadLines(pathToFile))
{
if (line.Contains("CustomerEN") && current == null)
{
current = new List<string>();
current.Add(line);
}
else if (line.Contains("CustomerEN") && current != null)
{
current.Add(line);
}
}
string s = String.Join(",", current);
MessageBox.Show(s);
foreach(File.ReadLines(pathToFile)中的var行)
{
if(line.Contains(“CustomerEN”)&¤t==null)
{
当前=新列表();
当前。添加(行);
}
else if(line.Contains(“CustomerEN”)&¤t!=null)
{
当前。添加(行);
}
}
字符串s=string.Join(“,”,当前);
MessageBox.Show(s);
你不需要知道索引。他需要找到第一个字符串,该字符串只出现在其他关键字之后。如果你愿意,你没有理由不能将计数器也放在foreach中。提出的每一个其他解决方案都会将整个文件读取到内存中,并一直保存到搜索完成为止。这是唯一一个在执行搜索时使用流仔细管理内存的方法。如果需要不区分大小写的搜索,请签出