I am having trouble attempting to find words in a text file in C#.
I want to find the word that is input into the console then display the entire line that the word
Iterate through all the lines (StreamReader, File.ReadAllLines, etc.) and check if
line.Contains("December")
(replace "December" with the user input).
Edit: I would go with the StreamReader in case you have large files. And use the IndexOf-Example from @Matias Cicero instead of contains for case insensitive.
Console.Write("Keyword: ");
var keyword = Console.ReadLine() ?? "";
using (var sr = new StreamReader("")) {
while (!sr.EndOfStream) {
var line = sr.ReadLine();
if (String.IsNullOrEmpty(line)) continue;
if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0) {
Console.WriteLine(line);
}
}
}