c#在txt文件中搜索字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12856471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:42:16  来源:igfitidea点击:

c# search string in txt file

c#

提问by nukleos

I want to find a string in txt file if string compares, it should go on reading lines till another string which I'm using as parameter.

如果字符串比较,我想在 txt 文件中找到一个字符串,它应该继续读取行直到我用作参数的另一个字符串。

Example:

例子:

CustomerEN //search for this string
...
some text wich has details about customer
id "123456"
username "rootuser"
...
CustomerCh //get text till this string

I need the details to work with them otherwise.

否则我需要详细信息才能与他们合作。

I'm using linq to search for "CustomerEN" like this:

我正在使用 linq 搜索“CustomerEN”,如下所示:

File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN"))

But now I'm stuck with reading lines (data) till "CustomerCh" to extract details.

但是现在我一直在阅读行(数据)直到“CustomerCh”来提取详细信息。

采纳答案by Rawling

If your pair of lines will only appear once in your file, you could use

如果您的这对行在您的文件中只出现一次,您可以使用

File.ReadLines(pathToTextFile)
    .SkipWhile(line => !line.Contains("CustomerEN"))
    .Skip(1) // optional
    .TakeWhile(line => !line.Contains("CustomerCh"));

If you could have multiple occurrences in one file, you're probably better off using a regular foreachloop - reading lines, keeping track of whether you're currently inside or outside a customer etc:

如果您可以在一个文件中多次出现,则最好使用常规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);
}

回答by Anton Sizikov

If you whant only one first string, you can use simple for-loop.

如果您只需要一个第一个字符串,则可以使用简单的 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];
   }
}

回答by Cristian Lupascu

With LINQ, you could use the SkipWhile/ TakeWhilemethods, like this:

使用 LINQ,您可以使用SkipWhile/TakeWhile方法,如下所示:

var importantLines = 
    File.ReadLines(pathToTextFile)
    .SkipWhile(line => !line.Contains("CustomerEN"))
    .TakeWhile(line => !line.Contains("CustomerCh"));

回答by Ekk

You have to use whilesince foreachdoes not know about index. Below is an example code.

您必须使用,while因为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();

回答by Tharmas

I worked a little bit the method that Rawling posted here to find more than one line in the same file until the end. This is what worked for me:

我使用了 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);