Home > Backend Development > C++ > How to Split Strings with Multiple Delimiters?

How to Split Strings with Multiple Delimiters?

Linda Hamilton
Release: 2024-11-11 04:03:03
Original
493 people have browsed it

How to Split Strings with Multiple Delimiters?

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));
}
Copy after login

Explanation:

  • We iterate over each line in the input string using std::getline.
  • For each line, we use std::find_first_of to locate the position of the first delimiter in the line.
  • If a delimiter is found, we add the substring between the previous delimiter and the current delimiter to the wordVector.
  • We then update prev to point to the character after the delimiter.
  • After processing the entire line, we add the remaining substring to the wordVector if it's not empty.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template