搜索特定字符串并返回整行

时间:2022-02-17 19:24:18

What I would like to do is find all instances of a string in a text file, then add the full lines containing the said string to an array.

我想要做的是在文本文件中找到字符串的所有实例,然后将包含所述字符串的完整行添加到数组中。

For example:

eng    GB    English
lir    LR    Liberian Creole English
mao    NZ    Maori

Searching eng, for example, must add the first two lines to the array, including of course the many more instances of 'eng' in the file.

例如,搜索eng必须将前两行添加到数组中,当然包括文件中的更多“eng”实例。

How can this be done, using a text file input and C#?

如何使用文本文件输入和C#完成此操作?

4 个解决方案

#1


17  

you can use TextReader to read each line and search for it, if you find what u want, then add that line into string array

你可以使用TextReader读取每一行并搜索它,如果你找到你想要的,然后将该行添加到字符串数组中

List<string> found = new List<string>();
string line;
using(StreamReader file =  new StreamReader("c:\\test.txt"))
{
   while((line = file.ReadLine()) != null)
   {
      if(line.Contains("eng"))
      {
         found.Add(line);
      }
   }
}

or you can use yield return to return enumurable

或者你可以使用收益率回报来回报

#2


8  

One line:

using System.IO;
using System.Linq;

var result = File.ReadAllLines(@"c:\temp").Select(s => s.Contains("eng"));

Or, if you want a more memory efficient solution, you can roll an extension method. You can use FileInfo, FileStream, etc. as the base handler:

或者,如果您想要更高效的内存解决方案,可以使用扩展方法。您可以使用FileInfo,FileStream等作为基本处理程序:

public static IEnumerable<string> ReadAndFilter(this FileInfo info, Predicate<string> condition)
{
    string line;

    using (var reader = new StreamReader(info.FullName))
    {
        while ((line = reader.ReadLine()) != null)
        {
            if (condition(line))
            {
                yield return line;
            }
        }
    }
}

Usage:

var result = new FileInfo(path).ReadAndFilter(s => s.Contains("eng"));

#3


0  

You can try the following code, i tried it and it was working

您可以尝试以下代码,我尝试了它,它正在工作

string searchKeyword = "eng";
string fileName = "Some file name here";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();

foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}

#4


0  

The File object contains a static ReadLines method that returns line-by-line, in contrast with ReadAllLines which returns an array and thus needs to load the complete file in memory.

File对象包含一个逐行返回的静态ReadLines方法,而ReadAllLines则返回一个数组,因此需要在内存中加载完整的文件。

So, by using File.ReadLines and LINQ an efficient and short solution could be written as:

因此,通过使用File.ReadLines和LINQ,可以将有效且简短的解决方案写成:

var found = File.ReadLines().Where(line => line.Contains("eng")).ToArray();

As for the original question, it could be optimized further by replacing line.Contains with line.StartsWith, as it seems the required term appears in the beginning of each line.

至于原始问题,它可以通过替换line进一步优化。包含line.StartsWith,因为似乎所需的术语出现在每一行的开头。

#1


17  

you can use TextReader to read each line and search for it, if you find what u want, then add that line into string array

你可以使用TextReader读取每一行并搜索它,如果你找到你想要的,然后将该行添加到字符串数组中

List<string> found = new List<string>();
string line;
using(StreamReader file =  new StreamReader("c:\\test.txt"))
{
   while((line = file.ReadLine()) != null)
   {
      if(line.Contains("eng"))
      {
         found.Add(line);
      }
   }
}

or you can use yield return to return enumurable

或者你可以使用收益率回报来回报

#2


8  

One line:

using System.IO;
using System.Linq;

var result = File.ReadAllLines(@"c:\temp").Select(s => s.Contains("eng"));

Or, if you want a more memory efficient solution, you can roll an extension method. You can use FileInfo, FileStream, etc. as the base handler:

或者,如果您想要更高效的内存解决方案,可以使用扩展方法。您可以使用FileInfo,FileStream等作为基本处理程序:

public static IEnumerable<string> ReadAndFilter(this FileInfo info, Predicate<string> condition)
{
    string line;

    using (var reader = new StreamReader(info.FullName))
    {
        while ((line = reader.ReadLine()) != null)
        {
            if (condition(line))
            {
                yield return line;
            }
        }
    }
}

Usage:

var result = new FileInfo(path).ReadAndFilter(s => s.Contains("eng"));

#3


0  

You can try the following code, i tried it and it was working

您可以尝试以下代码,我尝试了它,它正在工作

string searchKeyword = "eng";
string fileName = "Some file name here";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();

foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}

#4


0  

The File object contains a static ReadLines method that returns line-by-line, in contrast with ReadAllLines which returns an array and thus needs to load the complete file in memory.

File对象包含一个逐行返回的静态ReadLines方法,而ReadAllLines则返回一个数组,因此需要在内存中加载完整的文件。

So, by using File.ReadLines and LINQ an efficient and short solution could be written as:

因此,通过使用File.ReadLines和LINQ,可以将有效且简短的解决方案写成:

var found = File.ReadLines().Where(line => line.Contains("eng")).ToArray();

As for the original question, it could be optimized further by replacing line.Contains with line.StartsWith, as it seems the required term appears in the beginning of each line.

至于原始问题,它可以通过替换line进一步优化。包含line.StartsWith,因为似乎所需的术语出现在每一行的开头。