【模板小程序】字符串截取
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 }
『注:本文来自博客园“小溪的博客”,若非声明均为原创内容,请勿用于商业用途,转载请注明出处http://www.cnblogs.com/xiaoxi666/』
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性