C++ stl string 实现的字符串拆分函数

1 //字符串拆分
 2 void split(string s,char splitchar,vector<string>& vec)
 3 {
 4     if(vec.size()>0)//保证vec是空的
 5         vec.clear();
 6     int length = s.length();
 7     int start=0;
 8     string topush;
 9     for(int i=0; i<length; i++)
10     {
11         if(s[i] == splitchar && i == 0)//第一个就遇到分割符
12         {
13             start += 1;
14         }
15         else if(s[i] == splitchar)
16         {
17             topush=s.substr(start,i - start);
18             if(topush.length()>0)
19                 vec.push_back(topush);
20             start = i+1;
21         }
22         else if(i == length-1)//到达尾部
23         {
24             topush=s.substr(start,i+1 - start);
25             if(topush.length()>0)
26                 vec.push_back(topush);
27         }
28     }
29 }

原文链接: https://www.cnblogs.com/wzqx/p/5317020.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/230819

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午2:48
下一篇 2023年2月13日 下午2:48

相关推荐