Created
June 11, 2012 15:47
-
-
Save jacyzon/2910769 to your computer and use it in GitHub Desktop.
split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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