for example i have a string:
string s = "apple | orange | kiwi";
and i searched and there is a way:
stringstream stream(s);
string tok;
getline(stream, tok, '|');
but it only can return the first token "apple" I wonder that is there any way so it can return an array of string? Thank you. Let assume that the string s may be changed. For exammple, string s = "apple | orange | kiwi | berry";
As Benjamin points out, you answered this question yourself in its title.
#include <sstream>
#include <vector>
#include <string>
int main() {
// inputs
std::string str("abc:def");
char split_char = ':';
// work
std::istringstream split(str);
std::vector<std::string> tokens;
for (std::string each; std::getline(split, each, split_char); tokens.push_back(each));
// now use `tokens`
}
Note that your tokens will still have the trailing/leading <space>
characters. You may want to strip them off.
Since there is space between each word and |
, you can do this:
string s = "apple | orange | kiwi";
stringstream ss(s);
string toks[3];
string sep;
ss >> toks[0] >> sep >> toks[1] >> sep >> toks[2];
cout << toks[0] <<", "<< toks[1] <<", " << toks[2];
Output:
apple, orange, kiwi
Demo : http://www.ideone.com/kC8FZ
Note: it will work as long as there is atleast one space between each word and |
. That means, it will NOT work if you've this:
string s = "apple|orange|kiwi";
Boost is a good if you want robust solution. And if you don't want to use boost for whatever reason, then I would suggest you to see this blog:
Elegant ways to tokenize strings
It explains how you can tokenize your strings, with just one example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With