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

  1. #include <string>
  2. #include <vector>
  3. #include <algorithm>// std::replace_if
  4. using namespace std;
  5.  
  6. // std__string 字符串切割 - 漆天初晓 - 博客园.html (https://www.cnblogs.com/tyche116/p/9377330.html)
  7.  
  8. // 用单字符作为分隔
  9. vector<string> SplitStr(string strtem, char a)
  10. {
  11. vector<string> strvec;
  12.  
  13. string::size_type pos1, pos2;
  14. pos2 = strtem.find(a);
  15. pos1 = ;
  16. while (string::npos != pos2)
  17. {
  18. strvec.push_back(strtem.substr(pos1, pos2 - pos1));
  19.  
  20. pos1 = pos2 + ;
  21. pos2 = strtem.find(a, pos1);
  22. }
  23. strvec.push_back(strtem.substr(pos1));
  24. return strvec;
  25. }
  26.  
  27. // 由多个分隔符来分隔:
  28. std::vector<std::string> SplitString(string _strSrc, string _strDelimiter, bool _bRepeatedCharIgnored)
  29. {
  30. vector<string> resultStringVector;
  31. replace_if(_strSrc.begin(), _strSrc.end(),
  32. [&](const char& c)
  33. {
  34. if (_strDelimiter.find(c) != string::npos)
  35. { return true; } else { return false; }
  36. }/*pred*/, _strDelimiter.at());
  37.  
  38. //将出现的所有分隔符都替换成为一个相同的字符(分隔符字符串的第一个)
  39. size_t pos = _strSrc.find(_strDelimiter.at());
  40. std::string addedString = "";
  41. while (pos != std::string::npos) {
  42. addedString = _strSrc.substr(, pos);
  43. if (!addedString.empty() || !_bRepeatedCharIgnored) {
  44. resultStringVector.push_back(addedString);
  45. }
  46. _strSrc.erase(_strSrc.begin(), _strSrc.begin() + pos + );
  47. pos = _strSrc.find(_strDelimiter.at());
  48. }
  49. addedString = _strSrc;
  50. if (!addedString.empty() || !_bRepeatedCharIgnored) {
  51. resultStringVector.push_back(addedString);
  52. }
  53. return resultStringVector;
  54. }
  55.  
  56. /*
  57. C++ 分割字符串两种方法 - 盛开的石头 - 博客园.html(https://www.cnblogs.com/stonebloom-yu/p/6542756.html)
  58. 2、通过使用strtok()函数实现
  59. 原型:char *strtok(char *str, const char *delim);
  60. 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
  61. 描述:strtok()用来将字符串分割成一个个片段。
  62. 参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则会将该字符改为\0 字符。
  63. 在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。
  64. 其它:strtok函数线程不安全,可以使用strtok_r替代。
  65. */
  66. vector<string> SplitStr_S(const string &str, const string &pattern)
  67. {
  68. //const char* convert to char*
  69. char * strc = new char[strlen(str.c_str()) + ];
  70. strcpy(strc, str.c_str());
  71. vector<string> resultVec;
  72. char* tmpStr = strtok(strc, pattern.c_str());
  73. while (tmpStr != NULL)
  74. {
  75. resultVec.push_back(string(tmpStr));
  76. tmpStr = strtok(NULL, pattern.c_str());
  77. }
  78.  
  79. delete[] strc;
  80.  
  81. return resultVec;
  82. }
  83.  
  84. // ZC:上面是 查阅到的资料,下面是 我自己改编的代码
  85.  
  86. // ZC: 函数strtok(...)的参数 是只能一个字符作为分隔符?还是有什么通配符规则(类似正则表达式之类的)?没测试...
  87. void SplitStr_ZZ(vector<string>& _vtr, char* _pc, char* _pcPattern)
  88. {
  89. _vtr.clear();
  90. if ((_pc == NULL) || (_pcPattern == NULL))
  91. return;
  92.  
  93. char* pc = strtok(_pc, _pcPattern);
  94. while (pc != NULL)
  95. {
  96. _vtr.push_back(string(pc));
  97.  
  98. pc = strtok(NULL, _pcPattern);
  99. }
  100.  
  101. }

2、调用代码

  1. int main(int argc, char *argv[])
  2. {
  3. char* pc = (char*)"AAA=";
  4. int iLen = strlen(pc);
  5. char* pcc = new char[iLen +];
  6. pcc[iLen] = '\0';
  7. memcpy(pcc, pc, iLen);
  8.  
  9. vector<string> vtr;
  10. SplitStr_ZZ(vtr, pcc, (char*)"=");
  11. for (int i = ; i < vtr.size(); i++)
  12. {
  13. printf("%s\n", vtr.at(i).c_str());
  14. }
  15.  
  16. system("pause");
  17. return ;
  18. }

 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. harbor1.9仓库同步迁移

    harbor 1.9 实战的仓库迁移,过程实际上就是从A push 到B.16个tag 不到100G,挺快的 1分钟多. 假设我们从A迁移到B. 1.先在A上面建立一个目标仓库.    

  2. Codeforces Round #595 (Div. 3) A,B,C,D

    A题:n个学生,分成任意组,要求每组中任意两名学生的技能值相差不等于1,问最小分组. #include<bits/stdc++.h> using namespace std; #defin ...

  3. 【题解】Mountain Walking-C++

    题目题意翻译题意简述:现在给一个N*N的矩阵,找一条路径从左上角走到右下角,每次可以向上下左右四个方向中某个方向走.要求走过的点中,数字最大的减去最小的.要求值越小越好.现在就是要求这个值. 输入格式 ...

  4. 006_STM32程序移植之_SYN6288语音模块

    1. 测试环境:STM32C8T6 2. 测试模块:SYN6288语音模块 3. 测试接口: SYN6288语音模块: VCC------------------3.3V GND----------- ...

  5. loaction.reload(false)和location.reload(true)的区别

    loaction.reload(false)和location.reload(true)有差别啊,一个是先判断页面有没修改,有的话就从服务器下载页面,没有就直接从缓存里拿(这个会提升响应性能)而把该方 ...

  6. luogu P1553 数字反转(升级版)

    P1553 数字反转(升级版) 直通 思路: 首先使用char数组进行读入,然后直接按照题目要求进行反转即可, 但要注意的是对零的处理:(有点类似于高精去除前导零) ①去除只是整数.百分数的时候,反转 ...

  7. 安装更新npm和nodejs

    1.安装npm sudo apt-get install npm 2.升级npm sudo npm install npm@latest -g 3.安装用于安装nodejs的模块n sudo npm ...

  8. Luogu [P3622] [APIO2007]动物园

    题目链接 比较费脑子的一道题 先说题目核心思想 : 状压dp 环的处理我们先不管. 我们设 dp[j][s] 表示 到达动物 j 且 [ j , j+5) 这五个动物状态为s时 最多能使多少小朋友开心 ...

  9. selenium 定制启动chrome的参数

    selenium 定制启动chrome的参数 设置代理. 禁止图片加载 修改ua https://blog.csdn.net/vinson0526/article/details/51850929 1 ...

  10. HTTP header 介绍 转载

    这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...