Parse (split) a string in C++ using string delimiter (standard C++)

后端 未结 20 2121
时光说笑
时光说笑 2020-11-21 23:44

I am parsing a string in C++ using the following:

using namespace std;

string parsed,input=\"text to be parsed\";
stringstream input_stringstream(input);

i         


        
20条回答
  •  一向
    一向 (楼主)
    2020-11-21 23:55

    If you do not want to modify the string (as in the answer by Vincenzo Pii) and want to output the last token as well, you may want to use this approach:

    inline std::vector splitString( const std::string &s, const std::string &delimiter ){
        std::vector ret;
        size_t start = 0;
        size_t end = 0;
        size_t len = 0;
        std::string token;
        do{ end = s.find(delimiter,start); 
            len = end - start;
            token = s.substr(start, len);
            ret.emplace_back( token );
            start += len + delimiter.length();
            std::cout << token << std::endl;
        }while ( end != std::string::npos );
        return ret;
    }
    

提交回复
热议问题