Split String in C++

  1. Use the std::string::find and std::string::erase Functions to Split String in C++
  2. Use std::getline and erase-remove Idiom to Split String in C++
  3. Use std::istringstream With std::copy and std::istream_iterator to Split String in C++

This article will explain several methods of how to split a string in C++.

Use the std::string::find and std::string::erase Functions to Split String in C++

The find and erase functions are built-in members of the std::string class, and they can be combined to split the text into tokens delimited by the given characters. This method can be utilized to split a string by any user-declared substring or a single character. Still, the following example code demonstrates the scenario when the delimiter is a. find method returns a position of the substring if it is found or string::npos if not found. Note that we need to delete the processed part of the string plus the delimiter. Thus, the erase() function is called to deal with this task.

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
using std::istringstream;

int main(){
    string text = "Vivamus quis sagittis diam. "
                  "Cras accumsan, dui id varius "
                  "vitae tortor.";
    string delimiter = "a";
    vector<string> words{};

    size_t pos;
    while ((pos = text.find(delimiter)) != string::npos) {
        words.push_back(text.substr(0, pos));
        text.erase(0, pos + delimiter.length());
    }
    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Viv
mus quis s
gittis di
m. Cr
s
ccums
n, dui id v
rius vit

Use std::getline and erase-remove Idiom to Split String in C++

A similar method to solve the given problem is to use the std::getline function, which also can extract substrings between the delimiter that the user specifies. The next sample code splits the text on each space character and stores the extracted strings into a std::vector container. Note though, there is an erase-remove idiom used to delete any punctuation characters from the strings before they are stored.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
using std::istringstream;

int main(){
    string text = "Vivamus quis sagittis diam. "
                  "Cras accumsan, dui id varius "
                  "vitae tortor.";

    vector<string> words{};
    char delimiter = ' ';

    istringstream sstream(text);
    string word;
    while (std::getline(sstream, word, delimiter)) {
        word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
        words.push_back(word);
    }

    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Vivamus
quis
sagittis
diam
Cras
accumsan
dui
id
varius
vitae
tortor

Use std::istringstream With std::copy and std::istream_iterator to Split String in C++

Alternatively, one could initialize the std::istringstream object with the text that needs to be split and traverse it with std::istream_iterator. Note that this method can only split strings by spaces which is the default delimiter for the istream_iterator. Finally, we need to copy the extracted strings to the vector container, which is done using the std::copy algorithm.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
using std::istringstream;

int main(){
    string text = "Vivamus quis sagittis diam. "
                  "Cras accumsan, dui id varius "
                  "vitae tortor.";

    vector<string> words{};
    char delimiter = ' ';

    istringstream iss(text);
    copy(std::istream_iterator<string>(iss),
         std::istream_iterator<string>(),
         std::back_inserter(words));

    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Vivamus
quis
sagittis
diam.
Cras
accumsan,
dui
id
varius
vitae
tortor.
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Find Substring in String in C++
  • Remove Punctuation From a String in C++