stdstring - C++ Multiple delimiters with more than one char -
in c++, i'm having trouble coding multiple delimiters single char delimiters , string delimiters (e.g. "<=" delimiter opposed '='). code below works single char delimiters (i've set delimiters space, comma, dot, plus , equal) , separates words in string line nicely. however, don't know how add string delimiters code.
std::string delimiters = " ,.+=";//i want "<=" added single delimiter std::string line = "this+is,a.string=testing one"; std::size_t prev = 0, pos; while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos) { if (pos > prev) { cout << line.substr(prev, pos-prev) << endl; prev = pos + 1; } } if (prev < line.length()){ cout << line.substr(prev, std::string::npos) << endl; }
here 1 way erasing delimiters find line_copy string, having special if statement special delimiter. full example here:
auto pos = find_first_of(begin(line_copy), end(line_copy), begin(delimiters), end(delimiters)); while (pos != line_copy.end()) { if (pos != line_copy.end()) { if (*pos == '<' && *(pos + 1) == '=') { cout << "delimiter: \'"; cout << string(pos, pos + 2) << "\'" << endl; // remove delimiters copy string line_copy.erase(pos, pos + 2); } else { cout << "delimiter: \'" << *pos << "\'" << endl; // remove delimiters copy string line_copy.erase(pos, pos + 1); } } cout << endl; pos = find_first_of(begin(line_copy), end(line_copy), begin(delimiters), end(delimiters)); }
Comments
Post a Comment