Split on substring

前端 未结 4 2014
天涯浪人
天涯浪人 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:35

    Although boost::split indeed takes a predicate that operates on characters, there's a boost string algorithm that can split on substrings:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    int main()
    {
        std::string input = "message1foomessage2foomessage3";
    
        std::vector v;
        iter_split(v, input, boost::algorithm::first_finder("foo"));
    
        copy(v.begin(), v.end(), std::ostream_iterator(std::cout, " "));
        std::cout << '\n';
    }
    

提交回复
热议问题