某些文本输入导致数组越界

Certain text inputs cause array to be out of bounds

我想解析 CommandLine可以是两种格式:

  1. command 123 - 带有 1 参数的命令 (123)
  2. command 123,456 - 带有 2 参数的命令(123456

此处 command - 命令名称,后跟 space ' ' 和参数:123123,456 以逗号分隔 ,

我已尝试使用以下代码实现目标:

        for (int i = 0; i <= CommandLine.TextLength; i++)
        {
            String[] CommandLineText = CommandLine.Text.Split(' ');
            String Commands = CommandLine.Text.ToLower().Trim();
            String Command = CommandLineText[0];
            String Values = CommandLineText[1];
            String[] Parameters = Values.Split(',');
            int X = Int32.Parse(Parameters[0]);
            int Y = Int32.Parse(Parameters[1]);
        }

我遇到的问题是,当命令是第一种格式且只有 1 数字时, 第二个参数 变成 out界限.

假设您有一些命令接受 1 个参数,而其他命令接受 2 个参数...

您需要采取微小的步骤,以便您可以看到解析在什么时候出现故障。这也将允许您准确地告诉用户错误所在的位置:

private void button1_Click(object sender, EventArgs e)
{
    String rawCommand = CommandLine.Text.ToLower().Trim();
    if (rawCommand.Length > 0)
    {
        String[] parts = rawCommand.Split(' ');
        if (parts.Length >= 2)
        {
            String command = parts[0];
            String[] args = parts[1].Split(',');
            if (args.Length == 1)
            {
                String arg1 = args[0];
                int X;
                if (Int32.TryParse(arg1, out X))
                {
                    // ... do something in here with "command" and "X" ...
                    Console.WriteLine("Command: " + command);
                    Console.WriteLine("X: " + X);
                }
                else
                {
                    MessageBox.Show("Invalid X Argument!");
                }
            }
            else if(args.Length >= 2)
            {
                String arg1 = args[0];
                String arg2 = args[1];
                int X, Y;
                if (Int32.TryParse(arg1, out X) && Int32.TryParse(arg2, out Y))
                {
                    // ... do something in here with "command" and "X" and "Y" ...
                    Console.WriteLine("Command: " + command);
                    Console.WriteLine("X: " + X);
                    Console.WriteLine("Y: " + Y);
                }
                else
                {
                    MessageBox.Show("Invalid X and/or Y Argument!");
                }
            }
        }
        else
        {
            MessageBox.Show("Command has no Parameters!");
        }
    }
    else
    {
        MessageBox.Show("Missing Command!");
    }           
}

您似乎想要这样的东西:

  using System.Linq;

  ....

  string CommandLine = "doIt 123,456";

  ....

  // Given Command Line we split it by space into two parts:
  // 0. command name
  // 1. command arguments
  string[] parts = CommandLine
    .ToLower()
    .Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);

  // 1st part is a command name 
  command = parts[0];

  // 2nd part (if any) is parameters
  int[] Parameters = parts.Length <= 1
    ? new int[0]                        // We don't have parameters 
    : parts[1]
       .Split(',')                      // Split by comma
       .Select(item => int.Parse(item)) // Parse each argument as int
       .ToArray();                      // Organize them as an array

如果您坚持使用 XY,则必须确保 Parameters 足够长:

  // let X = -1 if Parameters doesn't have it
  int X = Parameters.Length >= 1 ? Parameters[0] : -1;

  // let Y = -1 if Parameters doesn't have it
  int Y = Parameters.Length >= 2 ? Parameters[1] : -1;