1

I was wondering if there is a way (built-in or implemented by a third party) to get a string and convert it in a string array like the Windows Cmd converts the arguments given to an application. So if I write for example goat "evil goat" -g, I want the string to convert to an array with three cells (goat, evil goat (not sure if quotation marks remain, but I think not), -g). I know I can implement it by hand, but I wonder if there is some way to do it automatically.

In case anyone is wondering, this is for a command-interpreter and the strings will be read from a TextBox.Text, using a Button.

Any link with relevant information is welcome!

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75

1 Answers1

1

You can use a regex to do what you want to do as follows:

public static List<string> GetCommandLineArguments(string commandline){
    var arguments = Regex.Matches(commandline, @"[\""].+?[\""]|[^ ]+")
                .Cast<Match>()
                .Select(m => m.Value)
                .ToList();
    return arguments;
}
  • 1
    Woah, this is an old question. Lemme try that out real quick! - This is probably what I ended up doing back when I asked this. Thanks anyways for the help! :) – Angelos Chalaris Jun 03 '16 at 09:16