Skip to content

Instantly share code, notes, and snippets.

@saga
Created January 4, 2010 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saga/268360 to your computer and use it in GitHub Desktop.
Save saga/268360 to your computer and use it in GitHub Desktop.
std::string split function
void split(const string& str, const string& delim, vector<string>* pParts)
{
size_t start, end = 0;
while (end < str.size()) {
start = end;
while (start < str.size() && (delim.find(str[start]) != string::npos))
{
start++; // skip initial whitespace
}
end = start;
while (end < str.size() && (delim.find(str[end]) == string::npos))
{
end++; // skip to end of word
}
if (end - start != 0) { // just ignore zero-length strings.
pParts->push_back(string(str, start, end-start));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment