Following code is fetching specific words from a text file.
Here is our Main method:
private static void Main(string[] args)
{
//Set the actual path from your system
const string filePath = @"container/myComments.txt";
const string singleline = "SingleLine";
const string multiline = "MultiLine";
var comemnts = new Comment();
Console.WriteLine("Single Line Comments : ");
Console.WriteLine(comemnts.GetCommentsInUpperCase(filePath, singleline));
Console.WriteLine("MultiLine Comments : ");
Console.WriteLine(comemnts.GetCommentsInUpperCase(filePath, multiline));
// Suspend the screen.
Console.ReadLine();
}Our Comment class:
public class Comment
{
public String GetCommentsInUpperCase(String filePath, String@
case)
{
string line;
var retunComments = String.Empty;
var sbSingleLine = new StringBuilder();
var sbMultiLine = new StringBuilder();
//Create streamReader object
var file = new StreamReader(filePath);
//Read line by line
while ((line = file.ReadLine()) != null)
{
line = line.TrimStart();
switch (@
case)
{
case "SingleLine":
if (line.Contains("//")) //for singleline comment
{
//Get rest part of line in uppercase and move the counter to next line
var strLine = line.Replace("//", "");
sbSingleLine.AppendLine(strLine.ToUpper());
//counter++;
}
retunComments = sbSingleLine.ToString();
break;
case "MultiLine":
if (line.StartsWith("/*")) //for multiple line comments
{
var strLine = line.Replace("/*", "");
sbMultiLine.AppendLine(strLine.ToUpper());
}
if (line.StartsWith("*") || line.EndsWith("*/")) //for between and termination of multiple line comments
{
var strLine = line.TrimStart('*').Replace("*/", "");
sbMultiLine.AppendLine(strLine.ToUpper());
}
retunComments = 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
sbMultiLine.AppendLine(line.TrimStart('*').ToUpper());
break;
}
}
file.Close();
return retunComments;
}
}Download the attached code and enjoy!
