Skip to content

Instantly share code, notes, and snippets.

@byBretema
Last active December 5, 2019 12:08
Show Gist options
  • Save byBretema/482d175c5e686dad4437cc3a7d9da135 to your computer and use it in GitHub Desktop.
Save byBretema/482d175c5e686dad4437cc3a7d9da135 to your computer and use it in GitHub Desktop.
Split an string in c++
std::vector<std::string> strSplit(const std::string& str,
const std::string& delimeter) {
std::string token;
std::vector<std::string> splitted;
size_t ini{0}, end{0};
while ((end = str.find(delimeter, ini)) <= str.size()) {
token = str.substr(ini, end - ini);
ini = end + delimeter.size();
splitted.push_back(token);
}
if (ini < str.size()) {
token = str.substr(ini);
splitted.push_back(token);
}
return splitted;
}
#include <string>
#include <vector>
std::vector<std::string> parse(const char* seps, std::string str) {
std::vector<std::string> out;
auto item = strtok(str.data(), seps);
while (item != NULL) {
out.push_back(item);
item = strtok(NULL, seps);
}
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment