Parse at ":" - Not even sure how to ask.. so here is an EX

Go To StackoverFlow.com

-3

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!

2012-04-04 21:12
by TomSwoobs
This is a very poorly expressed question. Think you can take a couple minutes to sort out exactly what you want - Sam Axe 2012-04-04 21:16
@Boo I've read it a couple of times. I'm confuse - Kevin 2012-04-04 21:16
I am not sure I understand what you are asking. You want the user to enter a multi-line text in a text box, each line in the form 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
Updated hope its more clear - TomSwoobs 2012-04-04 22:02


4

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.

2012-04-04 21:18
by Eric J.


0

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];
2012-04-04 21:17
by Ethan Brown


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.

2012-04-04 21:21
by Jetti
Little buggy, I think I know why.. because I don't have a lot of room to post here is a pastebin link of my comment

http://pastebin.com/a8pFMex - TomSwoobs 2012-04-04 21:36

@TomDoobies what is causing you problems specifically? I don't see any indication of where you are having problems on the pastebi - Jetti 2012-04-04 21:43
Look it needs to do this:

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

@TomDoobies You can combine this method with Eric J.'s response which will give you what you want. Also, I didn't write logic for you, I just wrote what will give you the results. If you have separate requirements then you will have to modify the sample to meet them - Jetti 2012-04-04 21:57
If you have Skype that would be fantastic - TomSwoobs 2012-04-04 21:57
@TomDoobies I do not have skyp - Jetti 2012-04-04 21:57
Ads