Last active
April 10, 2016 06:07
-
-
Save luuhq/8df4b07ea5f6babd8138b5a0cd35212b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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