stl string 使用指定的分隔符分割成数个子字符串
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void StringSplit(const string& str,vector<string>& vStr,const char& division)
{
int startPos = 0;
int endPos = string::npos;
startPos = str.find_first_not_of(division);
while(startPos != string::npos)
{
endPos = str.find_first_of(division,startPos);
if(endPos != string::npos)
{
string strSplit = str.substr(startPos,(endPos-startPos));
vStr.push_back(strSplit);
}else
{
string strSplit = str.substr(startPos);
vStr.push_back(strSplit);
}
startPos = str.find_first_not_of(division,endPos );
}
return;
}
void PrintElement(const string& str)
{
cout << str << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
string str1 = "_1_dfg45d#$__123456789_1_dfg45d#$__123456789_1_dfg45d#$__123456789_1_dfg45d#$__123456789_1_dfg45d#$__123456789";
string str2 = "__sdfsf_dfg45d#$__张三李四____sdfsf_dfg45d#$__张三李四____sdfsf_dfg45d#$__张三李四____sdfsf_dfg45d#$__张三李四__";
string str3 = "(*^(*^(*_dfg45d#$__天天向上__(*^(*^(*_dfg45d#$__天天向上__(*^(*^(*_dfg45d#$__天天向上__(*^(*^(*_dfg45d#$__天天向上__";
vector<string> vStr;
StringSplit(str1,vStr,'_');
for_each(vStr.begin(),vStr.end(),PrintElement);
cout << endl;
vStr.clear();
StringSplit(str2,vStr,'_');
for_each(vStr.begin(),vStr.end(),PrintElement);
cout << endl;
vStr.clear();
StringSplit(str3,vStr,'_');
for_each(vStr.begin(),vStr.end(),PrintElement);
cout << endl;
vStr.clear();
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
浙公网安备 33010602011771号