分割字符串

//字符串分割函数
std::vector<std::string> split(std::string str, std::string pattern)
{
    std::string::size_type pos;
    std::vector<std::string> result;
    str += pattern;//扩展字符串以方便操作
    int size = str.size();
    for (int i = 0; i < size; i++)
    {
        pos = str.find(pattern, i);
        if (pos < size)
        {
            std::string s = str.substr(i, pos - i);
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}

原文地址C++常用字符串分割方法(转) - ~小小鸟~ - 博客园 (cnblogs.com)

posted @ 2021-08-04 14:34  老婆饼里有老婆  阅读(108)  评论(0编辑  收藏  举报