Splitting Strings with Multiple Delimiters [Duplicate]
In text processing, the need often arises to divide a string into individual words or tokens. While standard string splitting techniques excel at handling single delimiters, the task becomes more challenging with multiple delimiters.
Problem:
Given a string and a set of delimiters, how can we split the string into words while considering all the specified delimiters?
Solution:
To effectively split a string using multiple delimiters, we leverage a combination of string stream and substring operations. Here's a modified version of the code provided:
std::stringstream stringStream(inputString); std::string line; while(std::getline(stringStream, line)) { std::size_t prev = 0, pos; auto delimiters = " ';"; // Customize delimiter string here while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos) { if (pos > prev) wordVector.push_back(line.substr(prev, pos-prev)); prev = pos+1; } if (prev < line.length()) wordVector.push_back(line.substr(prev, std::string::npos)); }
Explanation:
The above is the detailed content of How to Split Strings with Multiple Delimiters?. For more information, please follow other related articles on the PHP Chinese website!