Click here to Skip to main content
12,692,105 members (32,647 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Hello , can any one help how to remove comments quotes in c# in a file

For Example: In a File

//include studio.c

/*This is Example*/

This is second Example

OUTPUT should be :

include studio.c

This is Example

This is second Example

i tried using Remove(), IndexOf(), Replace() . but not getting , thanks in advance
Posted 4-Jan-13 6:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are trying perfectly right things. All you need it to put them together. :-)
Another idea: for performance sake, its much better to use the class System.Text.StringBuilder. It is mutable, but strings are immutable, so concatenating strings repeatedly will kill good performance. After parsing strings into some fragments, you Append then one by one to the instance of StringBuilder. To go back from StringBuilder to string, simply use ToString().

Abhinav S 4-Jan-13 12:25pm
My 5.
Thank you, Abhinav.
Marcus Kramer 4-Jan-13 13:05pm
Thank you, Marcus.
getanswer 8-Jan-13 12:07pm
Hi can anyone help me please how to write Parser for above question, thanks
What prevents you from writing the code. You know everything you need to know, just need to do the job. Or do you expect some of us to do this work for you?
If not, what else would you need?
getanswer 9-Jan-13 5:50am
i tried using parser

private string RemoveComments(string str) {
while (str.IndexOf("/*") != -1) {
str = str.Remove(str.IndexOf("/*"), str.IndexOf("*/") - str.IndexOf("/*") + 2);

while (str.IndexOf("//") != -1) {
str = str.Remove(str.IndexOf("//"), str.IndexOf(Environment.NewLine) - str.IndexOf("//"));
return str;

where am wrong please, let me know
It is for each line or whole text (then it would be wrong)? Where are all your lines, how do you find them?
Also, use Regex...
getanswer 9-Jan-13 10:40am
For whole Text, i want to search Comments and delete those line or lines(multiple) from Text,
//here is my Text file
string[] lines = File.ReadAllLines(@"D:\degagapr\Desktop\Test.txt");
string str=RemoveBlockComments(lines);

where am wrong please
OK, now I see. This is not so simple. First remove /**/ comments, ignoring //. You will need to keep the status "inInComment" inside the loop. Then, detect remaining // comments in each line and remove it to the end of the line. You are not doing it, as I cannot see string.Length anywhere in your code.


P.S.: after all, can you demonstrate some patience and do it all by yourself? The problem is just for the beginner...
getanswer 10-Jan-13 3:24am
Hi, i did its working for 2 comments i.e., /* */ , and // but not working for

This is second Example

can you please tell me how to check for this condition, i want to delete aboue comment, first i have to search this string with Comment and delete, thanks
Of course. Because you don't even try to do right thing.

I told you in previous comment. Please pay attention: 1) this kind of comment is not nested, 2) it absorbs // comments. So, you need to start with this. First of all, you need to scan all your lines in first pass and detect all points: going into comment, going out of comment. Remember all locations, separately from text. Consider it as a mode. Then do the rest.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100