delimiter token std code example
Example 1: split a string based on a delimiter in c++
void tokenize(string &str, char delim, vector<string> &out)
{
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}
int main()
{
string s="a;b;c";
char d=';';
vector<string> a;
tokenize(s,d,a);
for(auto it:a) cout<<it<<" ";
return 0;
}
Example 2: tokenize string c++
std::vector<std::string> tokenise(const std::string &str){
std::vector<std::string> tokens;
int first = 0;
while(first<str.size()){
int second = str.find_first_of(',',first);
if(second==std::string::npos){
second = str.size();
}
std::string token = str.substr(first, second-first);
tokens.push_back(token);
first = second + 1;
}
return tokens;
}