string的分割函数,返回是的分割后的的list,仿照VB和java的split函数
#include "stdafx.h"
#include<iostream>
#include<list>
using namespace std;
list<string> split(string str, string separator)
{
list<string> result;
int cutAt;
while( (cutAt = str.find_first_of(separator)) != str.npos )
{
if(cutAt > 0)
{
result.push_back(str.substr(0, cutAt));
}
str = str.substr(cutAt + 1);
}
if(str.length() > 0)
{
result.push_back(str);
}
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
string ppa = "1232###4546#546##46";
std::list<std::string> result = split(ppa,"#");
std::list<std::string>::iterator itr = result.begin();
for ( ; itr != result.end(); itr++)
{
cout<< (*itr).c_str() <<" " <<endl;
}
return 0;
}
可以识别连续的多个分割符。
结果:
1232
4546
546