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

后端 未结 20 2104
时光说笑
时光说笑 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-22 00:02

    This method uses std::string::find without mutating the original string by remembering the beginning and end of the previous substring token.

    #include 
    #include 
    
    int main()
    {
        std::string s = "scott>=tiger";
        std::string delim = ">=";
    
        auto start = 0U;
        auto end = s.find(delim);
        while (end != std::string::npos)
        {
            std::cout << s.substr(start, end - start) << std::endl;
            start = end + delim.length();
            end = s.find(delim, start);
        }
    
        std::cout << s.substr(start, end);
    }
    

提交回复
热议问题