Split on substring

前端 未结 4 1126
一向
一向 2020-12-15 00:58

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

    You could search for the next occurence of your substring thats used as split token. Such a method will probably return the index of the next occurence and having this you can split the string yourself.

    0 讨论(0)
  • 2020-12-15 01:39

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

    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <boost/algorithm/string/iter_find.hpp>
    #include <boost/algorithm/string/finder.hpp>
    int main()
    {
        std::string input = "message1foomessage2foomessage3";
    
        std::vector<std::string> v;
        iter_split(v, input, boost::algorithm::first_finder("foo"));
    
        copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << '\n';
    }
    
    0 讨论(0)
  • 2020-12-15 01:40

    It's a huge dependency, but I personally like Boost::Tokenizer.

    From the example on the page:

    // simple_example_1.cpp
    #include<iostream>
    #include<boost/tokenizer.hpp>
    #include<string>
    
    int main(){
       using namespace std;
       using namespace boost;
       string s = "This is,  a test";
       tokenizer<> tok(s);
       for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
           cout << *beg << "\n";
       }
    }
    
    0 讨论(0)
  • 2020-12-15 01:54

    As long as it concerns whitespace:

    string s("somethin\nsomethingElse");
    strinstream ss(s);
    string line;
    vector<string> 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<string> lines;
    string line;
    while( getline(ss,line) )
    {
        lines.push_back(line);
    }
    
    0 讨论(0)
提交回复
热议问题