Search code examples
c++stringsplitseparator

Splitting a string without losing the separator


I took this code for string split here: string split

char sep = ' ';
std::string s="1 This is an exampl";

for(size_t p=0, q=0; p!=s.npos; p=q)
std::cout << s.substr(p+(p!=0), (q=s.find(sep, p+1))-p-(p!=0)) << std::endl;

The code works ok and the output is:

1
This
is
an
exampl

If I change the separator from ' ' to 'e', the output is:

1 This is an
xampl

The 'e' in exampl is lost. How can I split the string using the same code but not losing the letter used as the separator?


Solution

  • I suggest splitting the string with a simple \b(?=e) regex (matching an e only if it is not preceded with a letter, digit or underscore):

    #include <string>
    #include <iostream>
    #include <regex>
    using namespace std;
    
    int main() {
        std::vector<std::string> strings;
        std::string s = "1 This is an exampl";
        std::regex re("\\b(?=e)");
        std::regex_token_iterator<std::string::iterator> it(s.begin(), s.end(), re, -1);
        decltype(it) end{};
        while (it != end){
            strings.push_back(*it++);
            std::cout << strings[strings.size()-1] << std::endl; // DEMO!
        }
        return 0;
    }
    

    See the C++ demo.