示例代码:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
vector<string> split(string str, string separator)
{
vector<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 main(void)
{
string test = "123,456,234,8765,432";
vector<string> test_s;
test_s = split(test, ",");
for (size_t i = 0; i<test_s.size(); i++)
{
cout << test_s[i] << endl;
}
}
标准输出:
123
456
234
8765
432