开发者

Split on substring

开发者 https://www.devze.com 2023-01-16 11:14 出处:网络
How would I split a string based on another substring in a simple way? e.g. split on \"\\r\\n\" message1\\r\\nmessage2

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

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

message1\r\nmessage2 

=>

message1
message2

From what I've 开发者_如何学JAVAbeen able to find both boost::tokenizer and boost::split only operates on single characters.

EDIT:

I'm aware that I could do this by using std::string::find and std::string::substr and have a loop etc... but thats not what I mean by "simple".


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';
}


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.


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";
   }
}


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

精彩评论

暂无评论...
验证码 换一张
取 消