So we have a simple split:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
vector<string> result;
if (delim.empty()) {
result.push_back(s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
or boost split. And we have simple main like:
int main() {
const vector<string> words = split("close no \"\n matter\" how \n far", " ");
copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
}
how to make it oputput something like
close
no
"\n matter"
how
end symbol found.
we want to introduce to split structures that shall be held unsplited and charecters that shall end parsing process. how to do such thing?
The following code:
generates:
Based on the examples you gave, you seemed to want newlines to count as delimiters when they appear outside of quotes and be represented by the literal
\nwhen inside of quotes, so that’s what this does. It also adds the ability to have multiple delimiters, such assplit_hereas I used the test.I wasn’t sure if you want unmatched quotes to be split the way matched quotes do since the example you gave has the unmatched quote separated by spaces. This code treats unmatched quotes as any other character, but it should be easy to modify if this is not the behavior you want.
The line:
will work on most, if not all, implementations of the STL, but it is not gauranteed to work. It can be replaced with the safer, but slower, version: