#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
功能:字符串分割
返回值: 分割后的字符串数组
参数:
str:原字符串
pattern:分隔符
*/
vector<string> split(const string &str, const string &pattern)
{
int current, pos, patlen;
vector<string> result;
patlen = pattern.length();
current = pos = 0;
while( current < str.length() )
{
pos = str.find(pattern, pos);
if( string::npos == pos )
{
result.push_back( str.substr(current) );
break;
}
if( current != pos )
result.push_back( str.substr(current, pos-current) );
pos += patlen;
current = pos;
}
return result;
}
//测试程序
int main()
{
string str = "AAsldfAAjsljAAsfjoeAAeorAsdofAA";
vector<string> vec = split(str, "AA");
for(int i=0; i<vec.size(); i++)
cout << vec[i] << endl;
return 0;
}
C++分割字符串
最新推荐文章于 2025-04-08 19:07:53 发布