12345678910111213141516171819202122232425262728293031
std::vector<std::string> splice(const std::string& str) { std::size_t pos=0,nextpos=0; std::vector<std::string> local; while(true) { nextpos=str.find('|',nextpos); if(nextpos==std::string::npos) {nextpos=str.size();} local.emplace_back(str,pos,nextpos); if(nextpos==str.size()) break; ++nextpos;pos=nextpos; } return local; } int main() { auto rslt=splice(std::string("@#XC322-xc|loc3455-xc|xc-0001xc|dr-loch-xc|fp2-09222-xc|dw-877str-xc|08188")); for(const auto& val : rslt) { std::cout<<val<<" "; }
@#XC322-xc loc3455-xc xc-0001xc dr-loch-xc fp2-09222-xc dw-877str-xc 08188
@#XC322-xc loc3455-xc|xc- xc-0001xc|dr-lo xc-0001xc|dr-loch-xc dr-loch-xc|fp2-09222-xc|dw-877str-xc dw-877str-xc|dw-877str-xc 08188
local.emplace_back(str,pos,nextpos - pos);
12
std::string temp = str.substr(pos, nextpos - pos); local.emplace_back(temp);