Last active
December 5, 2019 12:08
-
-
Save byBretema/482d175c5e686dad4437cc3a7d9da135 to your computer and use it in GitHub Desktop.
Split an string in c++
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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 <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