Search specific string and return whole line
Asked Answered
N

4

14

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.

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

Noncombatant answered 24/6, 2011 at 10:44 Comment(0)
R
20

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

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

Ritch answered 24/6, 2011 at 10:51 Comment(0)
M
11

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:

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"));
Maseru answered 24/6, 2011 at 10:57 Comment(0)
S
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);
    }
}
Separation answered 24/6, 2011 at 10:57 Comment(0)
S
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.

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

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.

Scyphus answered 29/1, 2018 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.