Following code is fetching specific words from a text file.
Here is our Main method:
- private static void Main(string[] args)
- {
-
- 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));
-
-
- 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();
-
- var file = new StreamReader(filePath);
-
-
- while ((line = file.ReadLine()) != null)
- {
- line = line.TrimStart();
-
- switch (@
- case)
- {
- case "SingleLine":
- if (line.Contains("//")) //for singleline comment
- {
-
- var strLine = line.Replace("//", "");
- sbSingleLine.AppendLine(strLine.ToUpper());
-
- }
-
- retunComments = sbSingleLine.ToString();
- break;
-
- case "MultiLine":
- if (line.StartsWith("/*"))
- {
- var strLine = line.Replace("/*", "");
-
- sbMultiLine.AppendLine(strLine.ToUpper());
- }
-
- if (line.StartsWith("*") || line.EndsWith("*/"))
- {
- var strLine = line.TrimStart('*').Replace("*/", "");
-
- sbMultiLine.AppendLine(strLine.ToUpper());
- }
-
-
- retunComments = sbMultiLine.ToString();
- break;
- default:
-
-
- sbMultiLine.AppendLine(line.TrimStart('*').ToUpper());
- break;
- }
- }
-
- file.Close();
-
- return retunComments;
- }
- }
Download the attached code and enjoy!