Searching for a Specific Word in a Text File and Displaying the line its on

后端 未结 4 640
小鲜肉
小鲜肉 2020-12-31 19:13

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

相关标签:
4条回答
  • 2020-12-31 19:35

    How about something like this:

    //We read all the lines from the file
    IEnumerable<string> lines = File.ReadAllLines("your_file.txt");
    
    //We read the input from the user
    Console.Write("Enter the word to search: ");
    string input = Console.ReadLine().Trim();
    
    //We identify the matches. If the input is empty, then we return no matches at all
    IEnumerable<string> matches = !String.IsNullOrEmpty(input)
                                  ? lines.Where(line => line.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0)
                                  : Enumerable.Empty<string>();
    
    //If there are matches, we output them. If there are not, we show an informative message
    Console.WriteLine(matches.Any()
                      ? String.Format("Matches:\n> {0}", String.Join("\n> ", matches))
                      : "There were no matches");
    

    This approach is simple and easy to read, it uses LINQ and String.IndexOf instead of String.Contains so we can do a case insensitive search.

    0 讨论(0)
  • 2020-12-31 19:38

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-31 19:40

    For finding text in a file you can use this algorithim use this code in

    static void Main(string[] args)
        {
        }
    

    try this one

    StreamReader oReader;
    if (File.Exists(@"C:\TextFile.txt")) 
    {
    Console.WriteLine("Enter a word to search");
    string cSearforSomething = Console.ReadLine().Trim();
    oReader = new StreamReader(@"C:\TextFile.txt");
    string cColl = oReader.ReadToEnd();
    string cCriteria = @"\b"+cSearforSomething+@"\b";
    System.Text.RegularExpressions.Regex oRegex = new 
    System.Text.RegularExpressions.Regex(cCriteria,RegexOptions.IgnoreCase);
    
    int count = oRegex.Matches(cColl).Count;
    Console.WriteLine(count.ToString());
    }
    Console.ReadLine();
    
    0 讨论(0)
  • 2020-12-31 19:47

    As mantioned by @Rinecamo, try this code:

    string toSearch = Console.ReadLine().Trim();
    

    In this codeline, you'll be able to read user input and store it in a line, then iterate for each line:

    foreach (string  line in System.IO.File.ReadAllLines(FILEPATH))
    {
        if(line.Contains(toSearch))
            Console.WriteLine(line);
    }
    

    Replace FILEPATH with the absolute or relative path, e.g. ".\file2Read.txt".

    0 讨论(0)
提交回复
热议问题