1、ZC:只测试使用了 自己改编的函数SplitStr_ZZ(...),其它的 未测试

#include <string>
#include <vector>
#include <algorithm>// std::replace_if
using namespace std; // std__string 字符串切割 - 漆天初晓 - 博客园.html (https://www.cnblogs.com/tyche116/p/9377330.html) // 用单字符作为分隔
vector<string> SplitStr(string strtem, char a)
{
vector<string> strvec; string::size_type pos1, pos2;
pos2 = strtem.find(a);
pos1 = ;
while (string::npos != pos2)
{
strvec.push_back(strtem.substr(pos1, pos2 - pos1)); pos1 = pos2 + ;
pos2 = strtem.find(a, pos1);
}
strvec.push_back(strtem.substr(pos1));
return strvec;
} // 由多个分隔符来分隔:
std::vector<std::string> SplitString(string _strSrc, string _strDelimiter, bool _bRepeatedCharIgnored)
{
vector<string> resultStringVector;
replace_if(_strSrc.begin(), _strSrc.end(),
[&](const char& c)
{
if (_strDelimiter.find(c) != string::npos)
{ return true; } else { return false; }
}/*pred*/, _strDelimiter.at()); //将出现的所有分隔符都替换成为一个相同的字符(分隔符字符串的第一个)
size_t pos = _strSrc.find(_strDelimiter.at());
std::string addedString = "";
while (pos != std::string::npos) {
addedString = _strSrc.substr(, pos);
if (!addedString.empty() || !_bRepeatedCharIgnored) {
resultStringVector.push_back(addedString);
}
_strSrc.erase(_strSrc.begin(), _strSrc.begin() + pos + );
pos = _strSrc.find(_strDelimiter.at());
}
addedString = _strSrc;
if (!addedString.empty() || !_bRepeatedCharIgnored) {
resultStringVector.push_back(addedString);
}
return resultStringVector;
} /*
C++ 分割字符串两种方法 - 盛开的石头 - 博客园.html(https://www.cnblogs.com/stonebloom-yu/p/6542756.html)
2、通过使用strtok()函数实现
原型:char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
描述:strtok()用来将字符串分割成一个个片段。
参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则会将该字符改为\0 字符。
在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。
其它:strtok函数线程不安全,可以使用strtok_r替代。
*/
vector<string> SplitStr_S(const string &str, const string &pattern)
{
//const char* convert to char*
char * strc = new char[strlen(str.c_str()) + ];
strcpy(strc, str.c_str());
vector<string> resultVec;
char* tmpStr = strtok(strc, pattern.c_str());
while (tmpStr != NULL)
{
resultVec.push_back(string(tmpStr));
tmpStr = strtok(NULL, pattern.c_str());
} delete[] strc; return resultVec;
} // ZC:上面是 查阅到的资料,下面是 我自己改编的代码 // ZC: 函数strtok(...)的参数 是只能一个字符作为分隔符?还是有什么通配符规则(类似正则表达式之类的)?没测试...
void SplitStr_ZZ(vector<string>& _vtr, char* _pc, char* _pcPattern)
{
_vtr.clear();
if ((_pc == NULL) || (_pcPattern == NULL))
return; char* pc = strtok(_pc, _pcPattern);
while (pc != NULL)
{
_vtr.push_back(string(pc)); pc = strtok(NULL, _pcPattern);
} }

2、调用代码

int main(int argc, char *argv[])
{
char* pc = (char*)"AAA=";
int iLen = strlen(pc);
char* pcc = new char[iLen +];
pcc[iLen] = '\0';
memcpy(pcc, pc, iLen); vector<string> vtr;
SplitStr_ZZ(vtr, pcc, (char*)"=");
for (int i = ; i < vtr.size(); i++)
{
printf("%s\n", vtr.at(i).c_str());
} system("pause");
return ;
}

 ZC:注意点:(1)传给 strtok(...)的第一个参数不能是 常量(PE结构常量段),会报内存错误

 ZC:  (2)若 分隔符后面没有内容了,或 未分割字符串(例如分隔符为"B"的情况),则 返回的 vector只包含一个元素

3、

4、

5、

C/C++.字符串分割的更多相关文章

  1. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  2. Oracle 超长字符串分割劈分

    Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...

  3. php学习零散笔记—字符串分割、fetch函数和单双引号。

    1 字符串分割——split()函数和preg_split()函数 split — 用正则表达式将字符串分割到数组中——貌似PHP5.3以上已不赞成使用 array split ( string $p ...

  4. 工作中用到的oracle字符串分割整理

    oracle部分: 定义类型(用于字符串分割): create or replace TYPE "STR_SPLIT" IS TABLE OF VARCHAR2 (4000); 字 ...

  5. Python 字符串分割的方法

    在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...

  6. 在C++中实现字符串分割--split

    字符串分割 在一些比较流行的语言中,字符串分割是一个比较重要的方法,不论是在python,java这样的系统级语言还是js这样的前端脚本都会在用到字符串的分割,然而在c++中却没有这样的方法用来调用. ...

  7. 随笔 JS 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里

    JS /* * 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里 * @id 要插入到DOM元素的ID * * 输入值为图片URL 字符串 * */ function addImages(i ...

  8. js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果

    使用的TP3.2 JS字符串分割成字符串数组 var images='{$content.pictureurl} ' ;结构是这样 attachment/picture/uploadify/20141 ...

  9. oracle根据分隔符将字符串分割成数组函数

    --创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...

  10. hive函数 -- split 字符串分割函数

    hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

随机推荐

  1. nginx中location的顺序(优先级)及rewrite规则写法

    一.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...

  2. 51nod 1843 排列合并机(DP+组合)

    题解链接 不过求ggg不用O(n2)DPO(n^2)DPO(n2)DP,g[n]g[n]g[n]直接就是卡特兰数的第n−1n-1n−1项.即: g[n]=(2(n−1)n−1)−(2(n−1)n−2) ...

  3. 自定义错误throw

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 14、Spring Boot 2.x 集成 Druid 数据源

    14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos

  5. [Algorithm] Tree Width with Level Width

    // --- Directions // Given the root node of a tree, return // an array where each element is the wid ...

  6. 003_软件安装之_Visual Studio 2012

    Visual Studio 2012安装,里面有视频教程,还有秘钥,连接失效联系我 2012版: 链接:https://pan.baidu.com/s/1BRE46cTKJW58YZ3lBFyjMw ...

  7. 025_自动为其他脚本添加解释器信息#!/bin/bash

    #!/bin/bash#先使用 grep 判断对象脚本是否已经有解释器信息,如果没有则使用 sed 添加解释器以及描述信息if ! grep -q "^#!" $1; then # ...

  8. SIGCHLD函数

    SIGCHLD的产生条件 子进程终止时 子进程接收到SIGSTOP信号停止时 子进程处在停止态,接受到SIGCONT后唤醒时 借助SIGCHLD信号回收子进程 子进程结束运行,其父进程会收到SIGCH ...

  9. 2019.7.9 校内测试 T2 极值问题

    这一次是交流测试?边交流边测试(滑稽 极值问题 乍一看这是一道数学题,因为1e9的数据让我暴力的心退却. 数学又不好,不会化简式子嘞,咋办? 不怕,咱会打表找规律.(考场上真的是打表找出了规律,打表打 ...

  10. 微信小程序之简单记账本开发记录(二)

    1.打开开发者工具 2.从微信公众平台上获取到appid,或者使用测试号. 项目的大致目录如下: 一个小程序主体部分由三个文件组成,必须放在项目的根目录中 以app为开头的文件名用来布置作用于整个项目 ...