How To Split A String By Multiple Delimiters In C++

Splitting the string in programming is a very common case. In solving many problems or optimizing the programs, programmers come across splitting the string. It can be done in many ways in C++. The different approaches will give different time and space complexity. This article will give some possible ways to split the string using the delimiters.

The delimiters are the characters used to mark the boundaries in the sentence. These are helpful while reading certain texts. Sometimes, these are used to express some meaning to the sentences. Some of the delimiters are space, comma, question mark, colon, semicolon, hyphen, forward slash, underscore, tab, exclamation marks, etc.

Example 1:

Let's take some examples to demonstrate how to split a string by multiple delimiters.

String: Hurry! We won the match, It is an achievement.

Delimiters: !,.

Output:

Hurry

We won the match

It is an achievement

Example 2:

String: Have a nice day

Delimiter: space

Output:

Below are some of the different ways to split the strings.

  • Using std::istringstream and std::getline:

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::istringstream and std::getline methods in C++.

Example

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
    vector<string> tokens;
    istringstream iss(input);
    string token;
    while (getline(iss, token)) {
        size_t start = 0, end = 0;
        while ((start = token.find_first_not_of(delimiters, end)) != string::npos) {
            end = token.find_first_of(delimiters, start);
            tokens.push_back(token.substr(start, end - start));
        }
    }
    return tokens;
}
int main() {
    string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
    vector<string> tokens = splitString(input, ",.!?-");
    for (const auto& token : tokens) {
        cout << token << endl;
    }
    return 0;
}

Output:

Explanation:

This program will split the static string by using the delimiters, which are stored in the vector. Here, the isstringstream is used to iterate through the each word and uses getline function to split the word.

  • Using std::regex

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::regex method in C++.

Example

#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
    regex re("[" + delimiters + "]+");
    vector<string> tokens(sregex_token_iterator(input.begin(), input.end(), re, -1), sregex_token_iterator());
    return tokens;
}
int main() {
    string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
    vector<string> tokens = splitString(input, ",.!-");
    for (const auto& token : tokens) {
        cout << token << endl;
    }
    return 0;
}

Output:

Explanation:

This program uses regular explanation to split the string in C++. Usually, the delimiters are stored in vector a, and regex is used to identify the delimiters in the string and for splitting.

  • Using std::string::findfirstof and std::string::substr:

Let's take an example to demonstrate how to split a string by multiple delimiters using the std::string::findfirstof and std::string::substr methods in C++.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
    vector<string> tokens;
    string token;
    size_t start = 0, end = 0;
    while ((end = input.find_first_of(delimiters, start)) != string::npos) {
        token = input.substr(start, end - start);
        if (!token.empty())
            tokens.push_back(token);
        start = end + 1;
    }
    token = input.substr(start);
    if (!token.empty())
        tokens.push_back(token);
    return tokens;
}

int main() {
    string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
    vector<string> tokens = splitString(input, ",.!-");
    for (const auto& token : tokens) {
        cout << token << endl;
    }
    return 0;
}

Output:

Explanation:

This program is another way to split the string. Here, we iterate through the input.find , the positions of the delimiters, and then extract the tokens from the string. After that, we push the sub-strings into the resultant vector, where the delimiters are removed.

  • Using Boost.Tokenizer:

Let's take an example to demonstrate how to split a string by multiple delimiters using Boost.Tokenizer method in C++.

Example

#include <iostream>
#include <string>
#include <vector>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
vector<string> splitString(const string& input, const string& delimiters) {
    vector<string> tokens;
    tokenizer<escaped_list_separator<char>> tknzr(input, escaped_list_separator<char>("", delimiters, ""));
    for (const string& token : tknzr)
        tokens.push_back(token);
    return tokens;
}
int main() {
    string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
    vector<string> tokens = splitString(input, ",.!-");
    for (const auto& token : tokens) {
        cout << token << endl;
    }
    return 0;
}

Output:

Explanation:

This program uses the Boost.Tokenizer to split the input here is the static string by delimiters specified in the delimiters vector. First, it creates the tokenizer object, and then uses that object and gives gives the string after splitting with multiple delimiters.

  • Splitting the string by using space as delimiter

Let's take an example to demonstrate how to split a string by multiple delimiters using space as delimiter in C++.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input) {
    vector<string> tokens;
    string currentToken;
    for (char ch : input) {
        if (ch == ' ') {
            if (!currentToken.empty()) {
                tokens.push_back(currentToken);
                currentToken.clear();
            }
        } else {
            currentToken += ch;
        }
    }
    if (!currentToken.empty()) {
        tokens.push_back(currentToken);
    }
    return tokens;
}
int main() {
    string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
    vector<string> tokens = splitString(input);
    for (const auto& token : tokens) {
        cout << token << endl;
    }
    return 0;
}

Output:

Conclusion:

In conclusion, this article will provide some of the ways to split the strings in an efficient way. These programs are related to C++. C++ has no particular in-built function to split the string using a delimiter. So, these methods are helpful while dealing with strings in C++ language programming.

Input Required

This code uses input(). Please provide values below: