Click here to Skip to main content
Rate this: bad
good
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
good
Please Sign up or sign in to vote.

Solution 1

You are trying perfectly right things. All you need it to put them together. Smile | :)
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().
 
—SA
  Permalink  
v2
Comments
Abhinav S at 4-Jan-13 12:25pm
   
My 5.
Sergey Alexandrovich Kryukov at 4-Jan-13 15:55pm
   
Thank you, Abhinav.
—SA
Marcus Kramer at 4-Jan-13 13:05pm
   
+5.
Sergey Alexandrovich Kryukov at 4-Jan-13 15:56pm
   
Thank you, Marcus.
—SA
getanswer at 8-Jan-13 12:07pm
   
Hi can anyone help me please how to write Parser for above question, thanks
Sergey Alexandrovich Kryukov at 8-Jan-13 12:36pm
   
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?
—SA
getanswer at 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
Sergey Alexandrovich Kryukov at 9-Jan-13 9:02am
   
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...
—SA
getanswer at 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
Sergey Alexandrovich Kryukov at 9-Jan-13 11:45am
   
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.
 
—SA
 
P.S.: after all, can you demonstrate some patience and do it all by yourself? The problem is just for the beginner...
getanswer at 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
Sergey Alexandrovich Kryukov at 10-Jan-13 9:49am
   
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.
 
—SA

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

  Print Answers RSS
0 OriginalGriff 7,105
1 DamithSL 5,079
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,617
4 Kornfeld Eliyahu Peter 4,384


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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