Say you have a text file you read in as a long string:
123 123 123
123 123 123
// Just a comment
123 123 123
123 123 123
# Just a comment
123 123 123
You typically split it in to lines something like this, (example in Unity3D),
List<string> lines = new List<string>(
controlFile.text.Split(new string[] { "\r","\n" },
StringSplitOptions.RemoveEmptyEntries));
.NET offers an incredible amount of string magic, such as formatting and more.
I wonder, is there some available magic to easily remove comments?
NOTE - of course one can do this using a regex, etc. As SonerGönül points out it can usefully be done with .Where
and .StartsWith
My question, is there some facility in the universe of .NET string magic, which specifically "understands" and helps with comments.
Even if the answer from an expert is "definitely no" that's a useful answer.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
You can try like this:
var t= Path.GetTempFileName();
var l= File.ReadLines(fileName).Where(l => !l.StartsWith("//") || !l.StartsWith("#"));
File.WriteAllLines(t, l);
File.Delete(fileName);
File.Move(t, fileName);
So you can basically copy the contents of your original file into a temp file which does not have comments line. Then delete the file and move the temp file to original file.
Hope that this will make sense:
string[] comments = { "//", "'", "#" };
var CommentFreeText = File.ReadLines("fileName Here")
.Where(X => !comments.Any(Y => X.StartsWith(Y)));
You can populate the comments[]
with the comment symbols that you want to remove from the textFile. While reading the text it will eliminate all the lines that start with any of the comment symbols.
And you can write it back using:
File.WriteAllLines("path", CommentFreeText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With