【代码片段】std::string 字符串分割

182 阅读1分钟

begin.C++字符串分如有疑问可留言沟通交流begin. C++字符串分割^{如有疑问可留言沟通交流}

Python 字符串分割方法 string.split(separator, max) 用着很方便,那C++呢?
下面调试了C++字符串分割方法如下:

// vector = 字符串分割(源字符串, 分隔符, 是否过滤空字符)
//        开启空字符过滤 - filterNullString = true
//        关闭空字符过滤 - filterNullString = false
std::vector<std::string> F_stringSplit(const std::string& stringSource,
                                       const std::string& splitSymbol,
                                       bool filterNullString = true) {    
    std::string::size_type pos;
    std::string str = stringSource;
    std::vector<std::string> result;
    str += splitSymbol;//扩展字符串以方便操作
    unsigned int size = str.size();
    for (unsigned int i = 0; i < size; i++) {
        pos = str.find(splitSymbol, i);
        if (pos < size) {
            std::string s = str.substr(i, pos - i);
            if (!filterNullString || s != "") {
                result.push_back(s);
            }
            i = pos + splitSymbol.size() - 1;
        }
    }
    return result;
}

end.友情链如有疑问可留言沟通交流end. 友情链接^{如有疑问可留言沟通交流}

  1. C++文章专栏
  2. 点云文章专栏
  3. Visual studio文章专栏
  4. ...

代码调试不易,转载请标明出处!
如果感觉本文对您有帮助,请留下您的赞,您的支持是我坚持写作分享的最大动力,谢谢!

References
0.Standard C++
1.cppreference.com
2.apiref.com/cpp-zh
3.cplusplus.com
4.C++ 标准库参考 | Microsoft Learn
5.C++ 教程 | 菜鸟教程
6.C++如何做字符串分割
7.C++中substr()函数用法
可以肯定的是学海无涯,这篇文章也会随着对 C++ 的深入学习而持续更新,
欢迎各位在评论区留言进行探讨交流。