Skip to content

Instantly share code, notes, and snippets.

@luuhq
Last active April 10, 2016 06:07
Show Gist options
  • Select an option

  • Save luuhq/8df4b07ea5f6babd8138b5a0cd35212b to your computer and use it in GitHub Desktop.

Select an option

Save luuhq/8df4b07ea5f6babd8138b5a0cd35212b to your computer and use it in GitHub Desktop.
public class Command
{
public string Action { get; set; }
public string Parameters { get; set; }
public static bool TryParse(string text, out Command command)
{
command = null;
if (string.IsNullOrWhiteSpace(text))
{
return false;
}
text = text.Trim();
command = new Command();
// Look for the first white space
var whiteSpaceIndex = text.IndexOf(' ');
if (whiteSpaceIndex == -1)
{
// No white space, treat the message as a command without parameter
command.Action = text.ToLowerInvariant();
return true;
}
// Extract the action & parameters
command.Action = text.Substring(0, whiteSpaceIndex).ToLowerInvariant();
command.Parameters = text.Substring(whiteSpaceIndex + 1);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment