.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Getting specific words from Text File using C#

Posted By:Gaurav Arora       Posted Date: August 22, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

Getting specific words from Text File using C#.

I have asked the same question from one of my readers with the following scenario:


I am creating a console application in which I need to get comments from a text file and display in a group of SingLine Comments and MultiLine Comments.

The following code snippet described the whole solution by-itself:

#region Author initials
/* How to get comments from
 * a text file
 * Author : Gaurav Arora
 * Date : July 12, 2009
 * Series: A Step Ahead Series*/

#region Namespaces Used
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReadingCommetFile.AstepAheadSeries
    #region Class displaying Comments
    class Program
        static void Main(string[] args)
            //Set the actual path from your system
            const string strFilePath = @"E:\Drive-F\My Articles\ReadingCommetFile\ReadingCommetFile\myComments.txt";
            ReturnComments objReturnComemnts = new ReturnComments();

            Console.WriteLine("Single Line Comments : ");
            String strSingleLine = objReturnComemnts.GetCommentsinUpperCase(strFilePath, "SingleLine");
            Console.WriteLine("MultiLine Comments : ");
            String strMultileLine = objReturnComemnts.GetCommentsinUpperCase(strFilePath, "MultiLine");

            // Suspend the screen.

    #region Class containing methods to read comments
    class ReturnComments
        public String GetCommentsinUpperCase(String strFilePath, String strCase)
            int counter = 0;
            string line;
            string strRetunComments = String.Empty;
            StringBuilder sbSingleLine = new StringBuilder();
            StringBuilder sbMultiLine = new StringBuilder();
            //Create streamReader object
            StreamReader file = new StreamReader(strFilePath);
            //Read line by line
            while ((line = file.ReadLine()) != null)
                switch (strCase)
                    case "SingleLine":
                        if (line.Contains("//")) //for singleline comment
                            //Get rest part of line in uppercase and move the counter to next line
                            String strLine = line.Replace("//", "");
                        strRetunComments = sbSingleLine.ToString();
                    case "MultiLine":
                        if (line.Contains("/*")) //for multiple line comments
                            String strLine = line.Replace("/*", "");
                        if (line.Contains("*/")) //Termination of multiple line comments
                            String strLine = line.Replace("*/", "");

                        strRetunComments = sbMultiLine.ToString();
                        //by default line which does not fall in above cases may be treated as a part of multiple comment
                        //in this case I supposed that this is a part of multiline comment

            sbSingleLine = null;
            sbMultiLine = null;

            return strRetunComments;
    /* Note : From the help of above you can also get the other details like Author Name, Description etc.
     * by Checking cases. */

Following is the final output:

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend