How to find the occurrences of string in a text file in C#

1 Answer

0 votes
using System;
using System.IO;

namespace Find_String_In_Text_File
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("d:\\wwwroot\\ajax.js");
            string s;

            while ((s = sr.ReadLine()) != null)
            {
                if ((s.IndexOf("getElementById")) != -1)
                {
                    Console.WriteLine("Found");
                    Console.WriteLine(s);
                    //break; // remove the comment to find only the first occurrence of string in a file
                }
            }
            sr.Close();				
        }
    }
}



answered Aug 22, 2014 by avibootz

Related questions

1 answer 178 views
1 answer 201 views
1 answer 228 views
1 answer 150 views
150 views asked Oct 30, 2014 by avibootz
2 answers 143 views
1 answer 103 views
...