Home > database >  Trying to parse multiple lines from a textbox
Trying to parse multiple lines from a textbox

Time:11-15

While this works for one line, i am trying to take multiple lines from the textbox "ProgramWindow" and run commands all of the commands typed one after another. in this case the commands draw shapes specified by the commands.
The commands can be in the following formats:

Command

Command Parameter

Command Parameter,Paramter

i have tried to use a loop but i am stuck at this point

String[] Lines = ProgramWindow.Text.Split('\n');
int NumberOfCommands = Lines.Length;
void Parse()
{    
     for (int i = 0; i <= NumberOfCommands; i  )
     {
            
                String[] Input = ProgramWindow.Text.ToLower().Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
                String Command = Input[0];
                int[] Parameters = Input.Length <= 1 ? new int[0] : Input[1].Split(',').Select(item => int.Parse(item)).ToArray();
                int X = Parameters.Length >= 1 ? Parameters[0] : 0;
                int Y = Parameters.Length >= 2 ? Parameters[1] : 0;
                Console.WriteLine(Command);
                Console.WriteLine(X);
                Console.WriteLine(Y);
                Commands(Command, X, Y);
                Refresh();
      }
}

CodePudding user response:

you have to use the Lines array

String[] Lines = ProgramWindow.Text.Split('\n');
int NumberOfCommands = Lines.Length;
void Parse()
{    
     for (int i = 0; i <= NumberOfCommands; i  )
     {
            
                String[] Input = Lines[i].ToLower().Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
                String Command = Input[0];
                int[] Parameters = Input.Length <= 1 ? new int[0] : Input[1].Split(',').Select(item => int.Parse(item)).ToArray();
                int X = Parameters.Length >= 1 ? Parameters[0] : 0;
                int Y = Parameters.Length >= 2 ? Parameters[1] : 0;
                Console.WriteLine(Command);
                Console.WriteLine(X);
                Console.WriteLine(Y);
                Commands(Command, X, Y);
                Refresh();
      }
}
  • Related