I have been trying to split a string by double newlines ("\n\n").
input_string = "firstline\nsecondline\n\nthirdline\nfourthline";
size_t current;
size_t next = std::string::npos;
do {
current = next + 1;
next = input_string.find_first_of("\n\n", current);
cout << "[" << input_string.substr(current, next - current) << "]" << endl;
} while (next != std::string::npos);
gives me the output
[firstline]
[secondline]
[]
[thirdline]
[fourthline]
which is obviously not what I wanted. I need to get something like
[first line
second line]
[third line
fourthline]
I have also tried boost::split but it gives me the same result. What am I missing?
find_first_ofonly looks for single characters. What you’re telling it to do by passing it"\n\n", is to find the first of either'\n'or'\n', and that’s redundant. Usestring::findinstead.boost::splitalso works by examining only one character at a time.