Searching for something with in a string C++

Started by
2 comments, last by the_edd 14 years, 5 months ago
I want to add some chat commands to my chat system. You type something in the chatbox and when you press enter it gets sent as a string to the server where the server handles it. But before it gets sent to the server I want to check the string for different things. So for example if you want to PM some one I want to be able to enter /pm [persons name] [message] then when you press enter have it check to see if theres the "/" in it then if so check to see what command follows it so in the example its pm. Then it would check to see who it is to and break that up into a string and then add the message to a different string. Then send it to the server in parts so the server can quickly take care of it. Is there any way to do that?
In Development:Rise of Heros: MORPG - http:www.riseofheroesmmo.com
Advertisement
A string tokenizer might come in handy. You could use it like this:
std::string s = "/pm name message";if( !s.empty() && s[0]=='/' ){//string starts with a slash, must be a command  //split it up into separate words  std::vector<std::string> tokens;  Tokenize( s, tokens );  int numTokens = tokens.size();  //if else if else... to find which command it is  if( tokens[0] == "/pm" )  {    if( numTokens > 2 )//enough parameters for this command?    {      std::string to = tokens[1];//read off the first parameter      //remove first 2 tokens, leave only the message      tokens.erase( tokens.begin(), tokens.begin()+1 );      SendPrivateMessage( tokens );    }    else      std::cout << "Not enough parameters" << std::endl;  }  else    std::cout << "unknown command" << std::endl;}else{//not a command, just regular chat  SendMessage( s );}
Thanks looks like that will do just what I need!
In Development:Rise of Heros: MORPG - http:www.riseofheroesmmo.com
If your "grammar" isn't too complicated, regular expressions might also be applicable.

This topic is closed to new replies.

Advertisement