Sign in to follow this  
EmpireProductions

Searching for something with in a string C++

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 );
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this