Split on substring

前端 未结 4 2018
天涯浪人
天涯浪人 2020-12-15 01:15

How would I split a string based on another substring in a simple way?

e.g. split on \"\\r\\n\"

message1\\r\\nmessage2 

=>

4条回答
  •  醉酒成梦
    2020-12-15 01:49

    As long as it concerns whitespace:

    string s("somethin\nsomethingElse");
    strinstream ss(s);
    string line;
    vector lines;
    while( ss >> line )
    {
        lines.push_back( line );
    }
    

    Alternatively, use getline(), which allows you to specify the tokenizing character as an optional third parameter:

    string s("Something\n\rOr\n\rOther");
    stringstream ss(s);
    vector lines;
    string line;
    while( getline(ss,line) )
    {
        lines.push_back(line);
    }
    

提交回复
热议问题