前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【模板小程序】字符串截取

【模板小程序】字符串截取

作者头像
xiaoxi666
发布2018-10-29 17:32:31
1.7K0
发布2018-10-29 17:32:31
举报
文章被收录于专栏:xiaoxi666的专栏xiaoxi666的专栏
代码语言:javascript
复制
 1 /*
 2 本程序说明:
 3 
 4 字符串截取,如字符串qwer kkk/f/lsj sdfgh pppi/uhgf根据"/"分割为qwer kkk、f、lsj sdfgh pppi、uhgf
 5 
 6 */
 7 #include <iostream>
 8 #include <vector>
 9 #include <string>
10 
11 using namespace std;
12 
13 //字符串截取
14 vector<string> split_string(const string& str, const string& pattern)
15 {
16     vector<string> resVec;
17 
18     if ("" == str)
19     {
20         return resVec;
21     }
22 
23     int index_start=0;
24     size_t pos = str.find(pattern,index_start);
25     while(pos!=string::npos)
26     {
27         resVec.push_back(str.substr(index_start,pos-index_start));
28         index_start=(pos+=pattern.length());
29         pos = str.find(pattern,index_start);
30     }
31     //截取最后一个
32     resVec.push_back(str.substr(index_start,str.length()-index_start));
33 
34     return resVec;
35 }
36 
37 int main()
38 {
39     //测试样例
40     //vector<string> resVec=split_string("qwer kkkflsj sdfgh pppiuhgf"," ");
41     //vector<string> resVec=split_string("qwer kkk/f/lsj sdfgh pppi/uhgf","/");
42     vector<string> resVec=split_string("qwer","/");
43     for(auto val:resVec)
44         cout<<val<<endl;
45 
46     return 0;
47 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档