CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    how to find and count specific word from txt file in C#

    I want to count number of occurences of "if" and "//" words from txt file using C# what's the best way to do this, need to show this count on console.
    also count how many blank lines are in the text file?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: how to find and count specific word from txt file in C#

    Use a FileStream to read in the file character by character, searching in between spaces. You could also read the file in as one huge string and use the String methods to find what you need. This would be easier, but you had better hope that the file isn't too large.

  3. #3
    Join Date
    Mar 2010
    Location
    India
    Posts
    3

    Re: how to find and count specific word from txt file in C#

    Using Collections / Generics concept we will achieve our criteria easily . Please check the below code and update as per your scenerio.

    1.) Initially read the text file (using filestream and imports IO namespace) ,
    System.IO.StreamReader StreamReader1 =
    new System.IO.StreamReader(Server.MapPath("test.txt"));

    2.) Append into the string builder.
    StringBuilder sbData = new StringBuilder();
    sbData.Append(StreamReader1.ReadToEnd());


    3.) Split the words from the stringbuilder (Spaces, Fullstops, Question Marks, Exclamations, Apostrophes, New Lines)
    string[] arrDataCollection = sbData.ToString().Split(' ');

    4.) Create dictionary for sortedlist.
    SortedList<int, string> slData = new SortedList<int, string>();

    5.) Loop the Spilt Data and get the distinct collections.
    for (int i = 0; i < Words.Length; i++)
    {
    slData.Add(i, arrDataCollection[i]);
    }

    6.) Get the distinct words, count and please save into one hashtable.
    Hashtable htCollections = new Hashtable();
    foreach (KeyValuePair<int, string> kvp in slData )
    {
    table.Add(kvp.Value, kvp.Key );
    }
    7.) Finally filter it from hashtable as per our requirements , Hope this will help you.

    Thanks,
    Vasanth

  4. #4
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: how to find and count specific word from txt file in C#

    Just want to say that HashTable is considered deprecated and has been replaced by the more generic solution of Dictionary<T,T>.

    From MSDN:
    The Dictionary class has the same functionality as the Hashtable class. A Dictionary of a specific type (other than Object) has better performance than a Hashtable for value types because the elements of Hashtable are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.

  5. #5
    Join Date
    Feb 2010
    Posts
    4

    Re: how to find and count specific word from txt file in C#

    Try to use Regex.
    Here is the sample


    static void Main(string[] args)
    {
    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();
    }

    hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured