Created
March 2, 2021 05:59
-
-
Save shawnfeng0/1b3362f4bee8427296beb9e028f87b74 to your computer and use it in GitHub Desktop.
string split function for 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
#include <vector> | |
#include <string> | |
inline std::vector<std::string> Split(const std::string &s, const std::string &delimiters = " ") { | |
std::vector<std::string> tokens; | |
std::string::size_type start = s.find_first_not_of(delimiters, 0); | |
std::string::size_type end = s.find_first_of(delimiters, start); | |
while (std::string::npos != end || std::string::npos != start) { | |
tokens.emplace_back(s.substr(start, end - start)); | |
start = s.find_first_not_of(delimiters, end); | |
end = s.find_first_of(delimiters, start); | |
} | |
return tokens; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment