I'm trying to parse the first words out of a string and i've comeup with this:
Would this be a good way to do it? All those calls to find and substr seems like it'd be slower than it needs to.Code:#include <string> #include <iostream> using namespace std; int main() { string Input = "/kick name the reason"; string Command, CommandTarget, CommandData, ParseString; ParseString = Input; Command = ParseString.substr(0, ParseString.find(" ")); ParseString = ParseString.substr(ParseString.find(" ")+1); CommandTarget = ParseString.substr(0, ParseString.find(" ")); ParseString = ParseString.substr(ParseString.find(" ")+1); CommandData = ParseString; cout << "Input: " << Input << endl; cout << "Command: " << Command << endl; cout << "Target: " << CommandTarget << endl; cout << "Data: " << CommandData << endl; Input = "/kick name"; ParseString = Input; Command = ParseString.substr(0, ParseString.find(" ")); ParseString = ParseString.substr(ParseString.find(" ")+1); CommandTarget = ParseString.substr(0, ParseString.find(" ")); ParseString = ParseString.substr(ParseString.find(" ")+1); CommandData = ParseString; cout << "Input: " << Input << endl; cout << "Command: " << Command << endl; cout << "Target: " << CommandTarget << endl; cout << "Data: " << CommandData << endl; system("pause"); return 0; }



LinkBack URL
About LinkBacks


