none
Best way to parse this string? RRS feed

  • Question

  • I'm working on a little "scripting" system for my XNA project that allows me to call C# methods and properties from the in-game console. The problem is, I have yet to develop a good command parser.

    Commands are written like this:

    command_name arg1, arg2, arg3, ...

    Basically, I need to extract command_name into a string and arg1, arg2, arg3 into an array of strings (split by commas). What's the best way to do this? Everything I have tried has either not worked or turned out really ugly and barely functional.

    Any help would be greatly appreciated!

    - David
    Thursday, December 27, 2007 9:35 PM

Answers

  • I would first split the command into two strings.  The first one you can get with substring .  Make the ending index of the substring command be the index of the first space.

     

    The second one you can also get with a substring command.  The only difference is that the starting index is the index of the first space plus one.  To split the second string into the arguments use the string.Split ( ) method.  The character to split on is a comma.  The split method will return an array of strings whose elements are defined at each comma.

     

    Hope this helps you out.

     

    Thursday, December 27, 2007 9:40 PM

All replies

  • I would first split the command into two strings.  The first one you can get with substring .  Make the ending index of the substring command be the index of the first space.

     

    The second one you can also get with a substring command.  The only difference is that the starting index is the index of the first space plus one.  To split the second string into the arguments use the string.Split ( ) method.  The character to split on is a comma.  The split method will return an array of strings whose elements are defined at each comma.

     

    Hope this helps you out.

     

    Thursday, December 27, 2007 9:40 PM
  • Thanks for the suggestions! Here's what I came up with:

    Code Block

    string command = "";
    string[] arguments = new string[] { };
    if (line.Contains(" ")) {
        command = line.Substring(0, line.IndexOf(' '));
        arguments = line.Substring(line.IndexOf(' ') + 1).Split(',');
        for (int i = 0; i < arguments.Length; i++) {
            arguments[i] = arguments[i].Trim();
        }
    } else {
        command = line;
    }


    Is there anything that needs to be improved or changed? I haven't done much work with string manipulation, so I probably made a stupid mistake or two. Wink
    Friday, December 28, 2007 12:02 AM
  • Here are a few observations:

     

    Code Block

    // leave these two uninitialized here so they are only initialized once

    string command;

    string[] arguments;

     

    // users are messy this Trim eliminates the empty argument if the user

    // types "command " (note the trailing space)

    line = line.Trim();

     

    // any time you use the return value of a function more then once, put it

    // in a temporary variable rather then calling the function multiple times

    int index = line.IndexOf(' ');

     

    // no need to call yet another function (Contains) when we already know

    // if it contains a space (from the index of the space character)

    if (index >= 0) {

       command = line.Substring(0, index);

       arguments = line.Substring(index + 1).Split(',');

       for (int i = 0; i < arguments.Length; i++)

          arguments[i] = arguments[i].Trim();

    }

    else {

       command = line;

       // personally I would set arguments to null and test it later

       // rather then allocate memory, but that's debatable

       arguments = new string[] {};

    }

     

     

     

    Friday, December 28, 2007 7:49 AM