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


Top 5 Contributors of the Month
david stephan

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:

Sir,

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*/
#endregion

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

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(strSingleLine);
            Console.WriteLine("MultiLine Comments : ");
            String strMultileLine = objReturnComemnts.GetCommentsinUpperCase(strFilePath, "MultiLine");
            Console.WriteLine(strMultileLine);
            Console.Read();

            // Suspend the screen.
            Console.ReadLine();
        }
    }
    #endregion

    #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("//", "");
                            strLine.ToUpper();
                            sbSingleLine.AppendLine(strLine);
                            counter++;
                        }
                        strRetunComments = sbSingleLine.ToString();
                        break;
                    case "MultiLine":
                        if (line.Contains("/*")) //for multiple line comments
                        {
                            String strLine = line.Replace("/*", "");
                            strLine.ToUpper();
                            sbMultiLine.AppendLine(strLine);
                            counter++;
                        }
                        if (line.Contains("*/")) //Termination of multiple line comments
                        {
                            String strLine = line.Replace("*/", "");
                            strLine.ToUpper();
                            sbMultiLine.AppendLine(strLine);
                            counter++;
                        }

                        strRetunComments = sbMultiLine.ToString();
                        break;
                    default:
                        //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
                        line.ToUpper();
                        sbMultiLine.AppendLine(line);
                        counter++;
                        break;
                }

            }
            sbSingleLine = null;
            sbMultiLine = null;
            file.Close();

            return strRetunComments;
        }
    }
    #endregion
    
    /* 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:

Responses

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