Getting specific words from Text File

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:

Final Output


Attachments

  • Fetching comments from File (30333-111458-ReadingCommentFile.rar)
  • Article by Gaurav Aroraa
    Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

    Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa

    Comments

    Author: Gaurav Aroraa12 Jul 2009 Member Level: Gold   Points : 0

    Rectified the code.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: