Splitting Strings

How to Split a String by a Delimiter in C++

C++ has no built-in split() for strings. Learn how to split a string by a delimiter with getline, find and substr, and std::views::split, with examples.

On this page
  1. What a delimiter is
  2. Split with getline and a stringstream
  3. Split on a longer delimiter with find and substr
  4. Split with std::views::split in C++20
  5. Which method to use
  6. Watch for spaces after the delimiter
  7. What to do with the pieces

C++ has no built-in function that splits a std::string into pieces, so you build the split from the standard library. std::getline with a std::stringstream works well for a single-character delimiter. find() with substr() handles a delimiter of any length. This guide shows both methods, a C++20 option, and how each one treats empty pieces.

What a delimiter is

A delimiter is the character or text that separates the pieces. In red,green,blue the delimiter is the comma, and splitting on it gives three pieces: red, green and blue. The delimiter is not part of any piece.

Split with getline and a stringstream

std::getline reads from a stream up to a delimiter and stores what it read in a string. Put the text in a std::stringstream, then call getline in a loop until it runs out of text:

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

std::vector<std::string> split(const std::string& text, char delimiter) {
    std::vector<std::string> parts;
    std::stringstream stream(text);
    std::string part;

    while (std::getline(stream, part, delimiter)) {
        parts.push_back(part);
    }
    return parts;
}

int main() {
    std::vector<std::string> fruits = split("apple,banana,cherry", ',');
    for (const std::string& fruit : fruits) {
        std::cout << fruit << '\n';
    }
}

The program prints:

apple
banana
cherry

Two details matter with this method:

  • An empty piece between two delimiters is kept. a,,b gives a, an empty string and b.
  • A delimiter at the very end adds no empty piece. a,b, gives only a and b.

getline takes a single character as its delimiter, so it cannot split on :: or another longer delimiter. Use the next method for that.

For more on how the stream class works, see stringstream in C++.

Split on a longer delimiter with find and substr

std::string::find returns the position where a piece of text first appears, or std::string::npos if it does not appear. substr copies part of a string, given a start position and a length. Together they step through the text one delimiter at a time:

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

std::vector<std::string> split(const std::string& text, const std::string& delimiter) {
    std::vector<std::string> parts;
    std::size_t start = 0;
    std::size_t end = text.find(delimiter);

    while (end != std::string::npos) {
        parts.push_back(text.substr(start, end - start));
        start = end + delimiter.length();
        end = text.find(delimiter, start);
    }
    parts.push_back(text.substr(start));
    return parts;
}

int main() {
    std::vector<std::string> items = split("one::two::three", "::");
    for (const std::string& item : items) {
        std::cout << item << '\n';
    }
}

This prints one, two and three, one per line. Unlike the getline version, a delimiter at the end gives an empty last piece, so a,b, becomes a, b and an empty string.

The delimiter must not be an empty string. With "", find returns the same position every time, and the loop never ends.

Split with std::views::split in C++20

C++20 added std::views::split to the ranges library. It splits a range by a value, and each piece is a view over the original text, not a new string. Copy a piece into a std::string when you need to keep it:

#include <iostream>
#include <ranges>
#include <string>

int main() {
    std::string text = "apple,banana,cherry";
    for (auto part : text | std::views::split(',')) {
        std::cout << std::string(part.begin(), part.end()) << '\n';
    }
}

Compile with -std=c++20 or a later standard. This version is short, but it depends on a standard library with ranges support.

Which method to use

  • A single character such as a comma, tab or newline: use std::getline with a std::stringstream.
  • A longer delimiter such as :: or , : use find() and substr().
  • C++20 or later, if you want the shortest loop: use std::views::split.
  • Whitespace only: use std::istringstream with >>. Each >> reads one word and skips any run of spaces, tabs or newlines, so no empty pieces appear.

Watch for spaces after the delimiter

Neither method removes spaces for you. The text apple, banana splits on the comma into apple and banana, and the second piece starts with a space. Trim each piece if the text has spaces after its delimiters.

What to do with the pieces

Each piece is still a string. If the pieces are numbers, convert them before you do arithmetic, as explained in how to convert a string to an int in c++. To join the pieces back into one string, see how to concatenate strings in c++. And if you are not yet comfortable looping over characters, start with how to iterate through a string in c++.

Use getline for simple, single-character delimiters, and find() when the delimiter is longer. Both use only the standard library and give you a std::vector<std::string> to work with. If you are coming from Python or C#, where split() is built in, how to use split() explains the C++ equivalent.

Keep exploring

Guides from other topics

All topics →

std::list in C++

std::list is the C++ standard library's doubly linked list. Learn to create one, add and remove elements, loop through it, and when to pick it over a vector.

std::list 5 min read

Tuples in C++ With std::tuple

Learn how std::tuple groups a fixed number of values of mixed types in C++. Covers creating tuples, reading them with std::get and structured bindings.

Tuples 4 min read