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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM