I would like to remove text between “–” and “\cr”.
I’m actually reading out of a file and if the file has a “–” in it, it should remove the “–” along with whatever everything until the “\cr”.
I am reading the file line by line.
using (StreamReader readFile = new StreamReader(filePath))
{
string line;
while ((line = readFile.ReadLine()) != null)
{
}
}
Ive tried using a substring to look for the characters
line.Substring(line.IndexOf("--"),line.IndexOf("\cr"));
But I am having a problem looking for the delimiters on each line
I was thinking about writing something like this
while ((line = readFile.ReadLine()) != null)
{
if (line.Substring(line.IndexOf("--")) // If it has "--"
{
//Then remove all text from between the 2 delimiters
}
}
Please help
Thanks
EDIT:
Problem solved, although I have come across another problem, I am unable to remove comments between /* */ as the comments occur on multiple lines. So I need to remove all text between /* */.
Any suggestions or help?
Thanks
A simple solution would be to just use a regex replace on the line:
This assumes that whatever you mean with
\cris the actual end of the line (which isn’t included anyway if you read it withReadLine()), so this removes everything from--until the end of the line instead.To replace
/* ... */comments too you may use:Quick PowerShell test: