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. 一款超好用的第三方评论插件--Gittalk

    使用GITALK的背景: 1. 最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我的 ...

  2. MySQL percona-toolkit工具详解

    一.检查和安装与Perl相关的模块 PT工具是使用Perl语言编写和执行的,所以需要系统中有Perl环境. 依赖包检查命令为: rpm -qa perl-DBI perl-DBD-MySQL perl ...

  3. python 关键参数和默认值

    def hello_key(greeting='hello', name='world'): print('%s, %s' % (greeting, name)) hello_key() hello_ ...

  4. netstat 基本用法

    Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字.如果你想确认系统上的 ...

  5. [Luogu] 最大收益

    题面:https://www.luogu.org/problemnew/show/P2647 题解:https://www.zybuluo.com/wsndy-xx/note/1142685

  6. [Luogu] 最小差值生成树

    https://www.luogu.org/recordnew/show/6125570 思路就是巧妙的枚举所有的生成树,取最优值首先按照边权排序找出第一颗最小生成树(l, r),其中l表示最小边的编 ...

  7. The 10th Shandong Provincial Collegiate Programming Contest

    目录 Contest Info Solutions A. Calandar B. Flipping Game C. Wandering Robot D. Game on a Graph E. BaoB ...

  8. [题解] [SDOI2017] 序列计数

    题面 题解 和 SDOI2015 序列统计 比较像 这个无非就是把乘改成了加, NTT 改成了 MTT 再加上了一个小小的容斥 : 拿所有方案减去不合法方案即可 Code #include <a ...

  9. Consul CAP理论纠错

    Consul CAP理论纠错 随便搜索Consul.zookeeper.etcd.eureka注册中心比较相关博客文章,你都会发现千篇一律的是以下这幅对比图:但是我对Consul使用的是CA架构还是C ...

  10. Java中RuntimeException和Exception

    在java的异常类体系中,Error和RuntimeException是非检查型异常,其他的都是检查型异常. 所有方法都可以在不声明throws的情况下抛出RuntimeException及其子类 不 ...