I have this code that I would like to parse by ":" it's hard to explain so hard to search so decided to ask my first question here
if (command == "hi")
{
MessageBox.Show(command);
skype.SendMessage(msg.Sender.Handle, nick + " Says: " + "what");
}
else { }
if (command == "what's up?")
{
MessageBox.Show(command);
skype.SendMessage(msg.Sender.Handle, nick + " Says: " + "nothing much");
}
else { }
Now what I want with the above is to have a richtextbox sort one line at a time so for EX:
hi:what
what's up?:nothing much
And is it possible every line that is made it ad's a new if statement rather then limiting users to a certain amount?
Edit: I need it to work like this-
Message | Response
hello:what's up?
The way the current code works is:
//if message "hi"
if (command == "hi")
{
//return this message if message "hi"
skype.SendMessage(msg.Sender.Handle, nick + " Says: " + "what");
}
Instead I would like a richtextbox to parse it at ":" and make each line a new if statement.
thank you!
question:answer
, and then the code should read these lines and for each of them execute an if
statement like those in your example - MiMo 2012-04-04 21:19
I'm really not certain what you're asking for, but my assumption is that you want to get a command and know how to respond to that command.
A straightforward way to do that is with a Dictionary<string, string>
.
Dictionary<string, string> commandsAndResponses = new Dictionary<string, string>();
commandsAndResponses.Add("hi", "what");
// Add the rest
if (commandsAndResponses.ContainsKey(command))
{
MessageBox.Show(command);
skype.SendMessage(msg.Sender.Handle, nick + " Says: " + commandsAndResponses[command]);
}
UPDATE
If you are getting your commands and responses from a RichTextBox as a ":" separated list of strings, you can use string.Split()
to build your dictionary.
It's not entirely clear to me what you're asking, but if I understand you correctly, you have a string that looks like "command:some other stuff"
and you want to separate the two parts. If my understanding is correct, it's very easy:
var originalString = "command:some other stuff";
var parts = originalString.Split(new[] {':'});
var command = parts[0];
It doesn't look like you really want if statements. You could do something like this:
string[] lines = richTextBox1.Text.Split('\n'); // split on line
foreach(string ln in lines)
{
string[] commands = ln.Split(':');
if(commands.Length == 2)
{
// first statement
skype.SendMessage(msg.Sender.Handle, string.Format("{0} Says: {1}",nick, command[0]);
// second statement
skype.SendMessage(msg.Sender.Handle, string.Format("{0} Says: {1}",nick, command[1]);
}
}
This will loop over each line in the textbox and then work on both parts of the command, if it is there is only one ":". I will leave it up to you whether or not you allow users to use ":" in text.
http://pastebin.com/a8pFMex - TomSwoobs 2012-04-04 21:36
message | respond Hello:Hey man
it just send's both the messages one at a time when no matter whats said - TomSwoobs 2012-04-04 21:47