use getline and while loop to split a string

后端 未结 2 636
醉话见心
醉话见心 2020-12-08 12:18

for example i have a string:

string s = \"apple | orange | kiwi\";

and i searched and there is a way:

stringstream stream(s         


        
相关标签:
2条回答
  • 2020-12-08 12:54

    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.

    0 讨论(0)
  • 2020-12-08 12:59

    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.

    0 讨论(0)
提交回复
热议问题