c++提取子串
来源:互联网 发布:淘宝网的经营模式 编辑:程序博客网 时间:2024/05/22 05:34
下面的函数GetSub可以从源字符串srcStr中获取所有以begPtn开始且以endPtn结束的子串,并将所获取的子串保存到vector里,例如
可以从字符串"sadfasdfsf#sdaf989789$#asdfasf894325445$#456123asdf$"中获取以“#”开头,以“$”结束的所有子串。
可以从字符串"sadfasdfsf#sdaf989789$#asdfasf894325445$#456123asdf$"中获取以“#”开头,以“$”结束的所有子串。
#include <iostream>#include <string.h>#include <vector>using namespace std;/* get all substrings which start with 'begPth' and end with 'endPtn' (but not include 'endPtn') from string srcStr,all of these substings will be stored in vector and then returned*/vector<string> GetSub(string srcStr,string begPtn,string endPtn){ vector<string> result; if(srcStr.empty()) return result; string::size_type iSrcLen = 0; iSrcLen = srcStr.size(); string::size_type iBegPos = 0, iEndPos = 0; while(iBegPos <= iSrcLen) { //从上个子串的结束位置开始查找下个字串的起始位置 iBegPos = srcStr.find(begPtn,iEndPos); if(iSrcLen <= iBegPos) break; //从子串的起始位置开始查找其结束位置 iEndPos = srcStr.find(endPtn,iBegPos+1); if(iEndPos < iBegPos) break; string subStr = srcStr.substr(iBegPos+1,iEndPos-iBegPos-1); result.push_back(subStr); } return result;}int main(){ string srcStr = "sadfasdfsf#sdaf989789$#asdfasf894325445$#456123asdf$"; string begPtn = "#"; string endPtn = "#"; vector<string> strlist = GetSub(srcStr,begPtn,endPtn); vector<string>::iterator iter_beg = strlist.begin(); vector<string>::iterator iter_end = strlist.end(); cout<<"srcStr = "<<srcStr.c_str()<<endl; cout<<"begPtn = "<<begPtn.c_str()<<" ; endPtn = "<<endPtn.c_str()<<endl; cout<<"begin out put:"<<endl; while(iter_beg < iter_end) { cout<<(*iter_beg).c_str()<<endl; iter_beg++; } char cStop; cout<<"stop...."<<endl; cin>>cStop; return 0;}
- 【C语言】 提取子字符串
- c++提取子串
- c++提取子串
- 提取子串-蓝桥杯
- 蓝桥杯 提取子串
- 提取子串
- 子串提取
- shell中提取子串
- shell中提取子串
- 字符串中提取子串
- 提取字符串最长子串
- 提取最长回文子串
- C#:从数组中提取子数组的扩展方法
- Javascript之提取一个子串
- 第三届蓝桥杯决赛 提取子串
- 正则表达式提取String子串
- C++参考demo-提取子串
- mmc提取子字符
- 稀疏矩阵的转置
- 灯火阑珊,一夜芳醉年华:QQ空间伤感日志
- 几种得到kernel32.dll地址的方法
- Great Trip
- shell 知识点补充(4)-date/数值运算/test 指令/判断符号 [ ]/预设变数($0)/条件判断:if then
- c++提取子串
- 如何看懂源代码--(分析源代码方法)
- 6名嫌犯兜卖假苹果手机遭拒改硬抢
- android学习笔记5 android按键事件处理代码走读[framework]
- 把悲伤封锁,把思念埋藏_伤感的回忆qq日志
- Linux Socket 学习(十)
- 数据结构算法----排序(4)----简单选择排序
- Trefis Team将百度目的价定替123美元
- exists in 区别