字符串分割函数(C++版本)

std::vector<std::string> StringSplit(const std::string &strSrc, const std::string &strSpliter)
{
    std::vector<std::string> resultVec;
    resultVec.reserve(10);

    std::basic_string<char>::size_type lastPos = std::string::npos;
    std::basic_string<char>::size_type pos = strSrc.find(strSpliter.c_str(), 0);

    while (pos != std::string::npos)
    {
        if (lastPos != std::string::npos)
            resultVec.push_back(strSrc.substr(lastPos + strSpliter.length(), pos - lastPos - strSpliter.length()));
        else
            resultVec.push_back(strSrc.substr(0, pos));

        lastPos = pos;
        pos = strSrc.find(strSpliter.c_str(), pos + strSpliter.length());
    }

    resultVec.push_back(strSrc.substr(lastPos + strSpliter.length()));

    return resultVec;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的字符串分割函数可以有多种实现方式。其中一种常见的方法是使用循环遍历字符串,根据指定的分隔符将字符串分割成多个子串。通过引用的代码示例,我们可以看到一个简单的C语言分割字符串函数实现。该代码使用了字符串的find和substr方法来进行分割操作。它将字符串分割成多个子串,并将这些子串存储在一个vector容器中。然后通过遍历该容器,将每个子串打印出来。 另一种实现方法是使用正则表达式,通过引用的代码示例,我们可以看到使用regex_token_iterator进行字符串分割的方式。该函数使用了istringstream作为输入流,以指定的分隔符为分隔符,通过getline方法将字符串分割成多个子串,并将每个子串输出。 综上所述,C语言中的字符串分割函数可以根据具体需求选择不同的实现方式,可以使用循环遍历字符串并使用字符串的find和substr方法,也可以使用正则表达式进行分割。具体的实现方式取决于项目需求和个人偏好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++string如何实现字符串分割函数split()——4种方法](https://blog.csdn.net/m0_58086930/article/details/122759927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值