暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

实现字符串split的6种方法

程序喵大人 2021-05-25
682
众所周知C++标准库没有提供std::string的split功能,究竟为什么没有提供split方法,我查了很多资料,网上也有很多说法,但是依旧没有找到官方答案。


既然没有,那我们不如......

按自己的方法实现一个好了。

如果项目库里集成了boost的话,可以直接使用boost的split功能,我这里也列出了6种实现split的方法,分享一下,希望大家能拓宽下思路。

方法1:stringstream和getline配合使用
std::vector<std::string> stringSplit(const std::string& str, char delim) {
    std::stringstream ss(str);
    std::string item;
    std::vector<std::string> elems;
    while (std::getline(ss, item, delim)) {
        if (!item.empty()) {
            elems.push_back(item);
        }
    }
    return elems;
}

方法2:使用std::string::find
std::vector<std::string> stringSplit(const std::string& str, char delim) {
    std::size_t previous = 0;
    std::size_t current = str.find(delim);
    std::vector<std::string> elems;
    while (current != std::string::npos) {
        if (current > previous) {
            elems.push_back(str.substr(previous, current - previous));
        }
        previous = current + 1;
        current = str.find(delim, previous);
    }
    if (previous != str.size()) {
        elems.push_back(str.substr(previous));
    }
    return elems;
}

方法3:使用std::string::find_first_of
std::vector<std::string> stringSplit(const std::string& str, char delim) {
    std::size_t previous = 0;
    std::size_t current = str.find_first_of(delim);
    std::vector<std::string> elems;
    while (current != std::string::npos) {
        if (current > previous) {
            elems.push_back(str.substr(previous, current - previous));
        }
        previous = current + 1;
        current = str.find_first_of(delim, previous);
    }
    if (previous != str.size()) {
        elems.push_back(str.substr(previous));
    }
    return elems;
}

方法4:使用C语言的strtok方法
std::vector<std::string> stringSplit(const std::string& strIn, char delim) {
    char* str = const_cast<char*>(strIn.c_str());
    std::string s;
    s.append(1, delim);
    std::vector<std::string> elems;
    char* splitted = strtok(str, s.c_str());
    while (splitted != NULL) {
        elems.push_back(std::string(splitted));
        splitted = strtok(NULL, s.c_str());
    }
    return elems;
}

方法5:std::string::find_first_of和std::string::find_first_not_of配合使用
std::vector<std::string> stringSplit(const std::string& str, char delim) {
    std::vector<std::string> elems;
    auto lastPos = str.find_first_not_of(delim, 0);
    auto pos = str.find_first_of(delim, lastPos);
    while (pos != std::string::npos || lastPos != std::string::npos) {
        elems.push_back(str.substr(lastPos, pos - lastPos));
        lastPos = str.find_first_not_of(delim, pos);
        pos = str.find_first_of(delim, lastPos);
    }
    return elems;
}

方法6:使用正则表达式
std::vector<std::string> stringSplit(const std::string& str, char delim) {
    std::string s;
    s.append(1, delim);
    std::regex reg(s);
    std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),
                                   std::sregex_token_iterator());
    return elems;
}

参考资料
https://www.zhihu.com/question/36642771
http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html


C++学习资料免费获取方法:关注程序喵大人,后台回复“程序喵”即可免费获取40万字C++进阶独家学习资料。





往期推荐


1、少写点
if-else吧,它的效率有多低你知道吗?
2、年度原创好文汇总
3、全网首发!!C++20新特性全在这一张图里了
4
他来了,他来了,C+
+17新特性精华都在这了
5、一文让你搞懂设计模式
6、C++11新特性,所有知识点都在这了!

文章转载自程序喵大人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论