C++字符串分割

很多人对C++的刻板印象是C++不适合处理字符串,其实C++也是有很方便的函数可以处理字符串的。下面我们来讲一下C++如何来对字符串进行分割。

使用strtok函数

使用strtok函数对字符串进行分割,该函数的声明如下:

char* strtok( char* str, const char* delim );

注意传入的参数是C风格的字符串,头文件为C的字符串库:<cstring>或者<string.h>。

下面是示例代码,示例代码尽可能使用C++风格的字符串,假设输入输出都是C++风格的字符串,因此会涉及到C风格字符串和C++风格字符串的转换:

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
    vector<string> result;
    string str = "A bird came down the walk";
    char *str_cpy = new char[str.size()];
    // convert const char* to char*
    strcpy(str_cpy, str.c_str());
    char *token = strtok(str_cpy, " ");
    while (token != NULL) {
        //cout << token << endl;
        result.push_back(string(token));
        token = strtok(NULL, " ");
    }

    for (auto r: result) {
        cout << r << endl;
    }

    return 0;
}

使用find函数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

std::vector<std::string> split(const std::string& s, const std::string &delim)
{
    std::vector<std::string> out;
    std::string::size_type beg = 0;
    for (auto end = 0; (end = s.find(delim, end)) != std::string::npos; ++end)
    {
        out.push_back(s.substr(beg, end - beg));
        beg = end + 1;
    }

    out.push_back(s.substr(beg));

    return out;
}

int main()
{
    std::string s = "read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]";
    //const char delim = ']';
    std::string delim = "]";
    std::vector<std::string> out;
    out = split(s, delim);

    for (auto &s: out) {
        std::cout << s << '\n';
    }

    return 0;
}

使用find_first_not_of和find函数

#include <iostream>
#include <string>
#include <vector>

void tokenize(std::string const &str, std::string delim,
            std::vector<std::string> &out)
{
    size_t start;
    size_t end = 0;

    while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
    {
        end = str.find(delim, start);
        out.push_back(str.substr(start, end - start));
    }
}

int main()
{
    std::string str = "I love you";
    int pos = str.find_first_not_of(" ");


    std::cout << pos << std::endl;

    return 0;
}

上面这两种方法思路基本一样,都是先查找第一个匹配的字符的位置,然后更新查找范围。

使用C++11正则表达式

#include <iostream>
#include <regex>
#include <string>

int main()
{
    std::string s = "C*C++*Java";
    std::regex regex("\\*");

    std::vector<std::string> out(
                    std::sregex_token_iterator(s.begin(), s.end(), regex, -1),
                    std::sregex_token_iterator()
                    );

    for (auto &s: out) {
        std::cout << s << '\n';
    }

    return 0;
}

参考资料

  1. https://en.cppreference.com/w/cpp/string/byte/strtok
  2. https://www.techiedelight.com/split-string-cpp-using-delimiter/
  3. https://en.cppreference.com/w/cpp/string/basic_string/find_first_not_of
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在一些编程练习中,经常会对字符串进行处理,往往处理之前都会对字符串进行分割来提取各部分信息。在C++中虽然没有像p...
    litexy阅读 75,938评论 0 10
  • 最近在研究C++,遇到一个问题需要将类似如下的字符串分割开:17;"_testCube##e17##13##Sma...
    藝龍阅读 568评论 0 0
  • //设计的string分割函数,使用一个vector存放每一个string,分割符也用一个string,对里面每个...
    XDgbh阅读 1,123评论 0 0
  • 在c语言中,字符串是用字符数组来存储的(并不像c++或者java等语言中有单独的string类型), 存放时在字符...
    朱森阅读 1,700评论 0 2
  • C语言中最常用标准库函数C++ sizeof的使用总结C++ Builder cstdlib 标准库函数相关颜色的...
    spfanlost阅读 16,172评论 0 3

友情链接更多精彩内容