How to find all lines containing a word in a text file in C#

1 Answer

0 votes
using System;
using System.IO;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            StringBuilder sb = new StringBuilder();

            using (System.IO.StreamReader file = new System.IO.StreamReader("d:\\test.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("Programming"))
                    {
                        sb.AppendLine(line.ToString());
                    }
                }
            }
            Console.WriteLine(sb.ToString());
        }
    }
}

/*

run:

C# Programming Is Very Nice
C# Programming Influenced from Java and Delphi Object Pascal

*/



answered Oct 31, 2014 by avibootz

Related questions

1 answer 209 views
1 answer 192 views
1 answer 174 views
1 answer 126 views
1 answer 155 views
155 views asked Mar 11, 2017 by avibootz
...