I am developing an application that reads and works with text files. These text files have the following structure:
** A comment
* A command
Data, data, data
** Some other comment
* Another command
1, 2, 3
4, 5, 6
I store the whole text file in memory by using string text = File.ReadAllText(file);
. However, I want to remove all lines that are a comment, i.e. all lines starting with "**"
.
This is achievable by the following method:
// this method also removes any white-spaces (this is intended)
string RemoveComments(string textWithComments)
{
string textWithoutComments = null;
string[] split = Regex.Split(text.Replace(" ", null), "\r\n|\r|\n").ToArray();
foreach (string line in split)
if (line.Length >= 2 && line[0] == '*' && line[1] == '*') continue;
else textWithoutComments += line + "\r\n";
return textWithoutComments;
}
However this is actually incredibly slow for big files. I also think it is possible to replace the whole method by a single line of code (possibly by using Regex). How can I achieve this (I have also never used regex).
PS: I also want to avoid StreamReader
s.
EDIT
An example file would look like this:
** Initial comment
*Command-0
** Some Comment: Header: Text
** Some text: text
*Command-1
**
** Some comment or text
**
*Command-2
*Command-3
1, 2, 3
2, 2, 4
3, 2, 5
** END COMMENT