How would I split a string based on another substring in a simple way?
e.g. split on \"\\r\\n\"
message1\\r\\nmessage2
=>
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.
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';
}
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);
}