c++ - Shortest code to split std::string into std::pair -
i have text, in "key:value" format. actual text may like, "server: nginx"
or "server:nginx"
ignoring whitespaces between key, :
, value.
what fastest , shortest way split std::pair<std::string, std::string>
?
david close, didn't test code.
here's working version.
auto index = str.find(':'); std::pair<std::string,std::string> keyval; if (index != std::string::npos) { // split around ':' character keyval = std::make_pair( str.substr(0,index), str.substr(index+1) ); // trim leading ' ' in value part // (you may wish add further conditions, such '\t') while (!keyval.second.empty() && keyval.second.front() == ' ') { keyval.second.erase(0,1); } }
Comments
Post a Comment