C/C++.字符串分割
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++.字符串分割的更多相关文章
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- Oracle 超长字符串分割劈分
Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...
- php学习零散笔记—字符串分割、fetch函数和单双引号。
1 字符串分割——split()函数和preg_split()函数 split — 用正则表达式将字符串分割到数组中——貌似PHP5.3以上已不赞成使用 array split ( string $p ...
- 工作中用到的oracle字符串分割整理
oracle部分: 定义类型(用于字符串分割): create or replace TYPE "STR_SPLIT" IS TABLE OF VARCHAR2 (4000); 字 ...
- Python 字符串分割的方法
在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...
- 在C++中实现字符串分割--split
字符串分割 在一些比较流行的语言中,字符串分割是一个比较重要的方法,不论是在python,java这样的系统级语言还是js这样的前端脚本都会在用到字符串的分割,然而在c++中却没有这样的方法用来调用. ...
- 随笔 JS 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里
JS /* * 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里 * @id 要插入到DOM元素的ID * * 输入值为图片URL 字符串 * */ function addImages(i ...
- js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果
使用的TP3.2 JS字符串分割成字符串数组 var images='{$content.pictureurl} ' ;结构是这样 attachment/picture/uploadify/20141 ...
- oracle根据分隔符将字符串分割成数组函数
--创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...
- hive函数 -- split 字符串分割函数
hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
随机推荐
- harbor1.9仓库同步迁移
harbor 1.9 实战的仓库迁移,过程实际上就是从A push 到B.16个tag 不到100G,挺快的 1分钟多. 假设我们从A迁移到B. 1.先在A上面建立一个目标仓库.
- Codeforces Round #595 (Div. 3) A,B,C,D
A题:n个学生,分成任意组,要求每组中任意两名学生的技能值相差不等于1,问最小分组. #include<bits/stdc++.h> using namespace std; #defin ...
- 【题解】Mountain Walking-C++
题目题意翻译题意简述:现在给一个N*N的矩阵,找一条路径从左上角走到右下角,每次可以向上下左右四个方向中某个方向走.要求走过的点中,数字最大的减去最小的.要求值越小越好.现在就是要求这个值. 输入格式 ...
- 006_STM32程序移植之_SYN6288语音模块
1. 测试环境:STM32C8T6 2. 测试模块:SYN6288语音模块 3. 测试接口: SYN6288语音模块: VCC------------------3.3V GND----------- ...
- loaction.reload(false)和location.reload(true)的区别
loaction.reload(false)和location.reload(true)有差别啊,一个是先判断页面有没修改,有的话就从服务器下载页面,没有就直接从缓存里拿(这个会提升响应性能)而把该方 ...
- luogu P1553 数字反转(升级版)
P1553 数字反转(升级版) 直通 思路: 首先使用char数组进行读入,然后直接按照题目要求进行反转即可, 但要注意的是对零的处理:(有点类似于高精去除前导零) ①去除只是整数.百分数的时候,反转 ...
- 安装更新npm和nodejs
1.安装npm sudo apt-get install npm 2.升级npm sudo npm install npm@latest -g 3.安装用于安装nodejs的模块n sudo npm ...
- Luogu [P3622] [APIO2007]动物园
题目链接 比较费脑子的一道题 先说题目核心思想 : 状压dp 环的处理我们先不管. 我们设 dp[j][s] 表示 到达动物 j 且 [ j , j+5) 这五个动物状态为s时 最多能使多少小朋友开心 ...
- selenium 定制启动chrome的参数
selenium 定制启动chrome的参数 设置代理. 禁止图片加载 修改ua https://blog.csdn.net/vinson0526/article/details/51850929 1 ...
- HTTP header 介绍 转载
这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...