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

后端 未结 4 694
小鲜肉
小鲜肉 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: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();
    

提交回复
热议问题