c++提取子串
来源:互联网 发布:淘宝网cf武器 编辑:程序博客网 时间:2024/06/06 03:28
下面的函数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提取子字符
- uC/OS-II内核架构解析(6)---uC/OS-II内存管理
- MVC与MFC设计模式详解
- C++ template 学习笔记
- c语言的输入输出
- 今天我在IIS里发布网站,遇到了4个问题,将其解答发出来,顺便可以作为发布网站的一般检查步骤
- c++提取子串
- uC/OS-II内核架构解析(7)---uC/OS-II应用开发
- blog的第一篇
- python 递归——冪
- win8系统U盘安装办法
- 树状数组
- 浮华沧桑的伤感完美日志发布:有伤痕,才算完美
- 多线程与串行的速度比较
- JAVA======加载图片示例