简单的string split

 
 /**
* Split string on delim.
*
* @param The string to be split.
* @param The separator characters.
* @returns Vector with the parts of the string split up. Empty strings will not remove from result.
*/
std::vector<std::string> split_string(const std::string& src_str, const char* delim)
{
    std::vector<std::string> tokens;
    if (src_str.empty() || nullptr == delim)
        return tokens;
 
    size_t delim_len = strlen(delim);
    std::string::size_type pos = 0;
    while(pos < src_str.size())
    {
        std::string::size_type next_pos = src_str.find(delim, pos);
        if(std::string::npos == next_pos)
            next_pos = src_str.size();
 
        if(pos < next_pos)
            tokens.push_back(src_str.substr(pos, next_pos - pos));
 
        if(pos == next_pos)
            tokens.push_back("");
 
        pos = next_pos + delim_len;
    }
 
    return tokens;
}
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/water-bear/p/12132746.html