Skip to content

Instantly share code, notes, and snippets.

@jacyzon
Created June 11, 2012 15:47
Show Gist options
  • Save jacyzon/2910769 to your computer and use it in GitHub Desktop.
Save jacyzon/2910769 to your computer and use it in GitHub Desktop.
split
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
vector<string> split(const string& src, string separate_character)
{
vector<string> strs;
int separate_characterLen = separate_character.size();
int lastPosition = 0,index = -1;
while (-1 != (index = src.find(separate_character,lastPosition)))
{
strs.push_back(src.substr(lastPosition,index - lastPosition));
lastPosition = index + separate_characterLen;
}
string lastString = src.substr(lastPosition);
if (!lastString.empty())
strs.push_back(lastString);
return strs;
}
int main()
{
string s = "123,456,789,0,888";
string del = ",";
vector<string> strs = split(s, del);
for ( unsigned int i = 0; i < strs.size(); i++)
{
cout << strs[i].c_str() << endl;
}
return system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment