I am trying to make a simple script interpreter in my application. A valid script to use in my application will be something like:
#this is a comment
*IDN? #this is a comment after a valid command
:READ? #this was also a comment after a valid command
RST
#last line was a valid comment but with no comment!
Now after loading the script content inside an array of string I want to execute each line, if it does not start with # and also ignor # in the same line if it exsist:
foreach(var command in commands)
{
if(!command.StartsWith("#"))
{
_handle.WriteString(command);
}
}
My code will take care of starting comments. But how to check for inline comments?
I got this idea, Will this code be IDIOT-PROOF ?
foreach(var command in commands)
{
if(!command.StartsWith("#"))
{
if(command.IndexOf('#') != null)
{
_handle.WriteString(command.Remove(command.IndexOf('#')));
}
else
_handle.WriteString(command);
}
}
Rephrase your question – another way to phrase it is that you want to process the part of each line that appears before the first
#sign. Which would be the first element of the array returned byString.Split:And this now deals with the
#character, wherever it appears in the line.