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);    }  } 

(live demo)


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -