Skip to content

Instantly share code, notes, and snippets.

@utilForever
Created April 1, 2021 15:46
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 utilForever/196c37b01c1e4209ae0db8e1dedafc9d to your computer and use it in GitHub Desktop.
Save utilForever/196c37b01c1e4209ae0db8e1dedafc9d to your computer and use it in GitHub Desktop.
Split String in C++
//! Splits a string \p str using \p delim.
//! \param str An original string.
//! \param delim A string delimiter to split.
//! \return A splitted string.
inline std::vector<std::string> SplitString(const std::string& str,
const std::string& delim)
{
std::vector<std::string> tokens;
std::size_t prev = 0, pos;
do
{
pos = str.find(delim, prev);
if (pos == std::string::npos)
{
pos = str.length();
}
std::string token = str.substr(prev, pos - prev);
if (!token.empty())
{
tokens.push_back(token);
}
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment