Skip to content
C++ Better Explained
Go back
C++ Split String: How to Split a String by Delimiter (3 Ways)
Edit page

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

To split a string in C++, put it in a std::stringstream and read pieces out with std::getline(ss, token, delimiter). C++ has no built-in split() like Python, but a few lines of code handle every case — splitting by spaces, by a single character, or by a longer substring.


Method 1: Split by Whitespace

If you just want the words in a sentence, a stringstream with the >> operator splits on any whitespace automatically:

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

int main() {
    std::string text = "the quick brown fox";
    std::stringstream ss(text);
    std::vector<std::string> words;

    std::string word;
    while (ss >> word) {          // >> skips whitespace
        words.push_back(word);
    }

    for (const std::string& w : words)
        std::cout << w << "\n";
    return 0;
}

The >> operator reads one whitespace-separated token at a time and stops at the end, so words ends up holding the, quick, brown, fox. This is the cleanest option when your separator is spaces or tabs.


Method 2: Split by a Single Character (like a comma)

For a specific delimiter such as a comma, use std::getline with a third argument — the character to split on:

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

std::vector<std::string> split(const std::string& s, char delim) {
    std::vector<std::string> parts;
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        parts.push_back(item);
    }
    return parts;
}

int main() {
    for (const std::string& p : split("apple,banana,cherry", ','))
        std::cout << p << "\n";
    return 0;
}

std::getline(ss, item, ',') reads characters until it hits a comma, stores everything before it in item, and discards the comma. Looping until it returns false walks through the whole string. Pulling this into a reusable split function keeps your code clean — this is the workhorse method you’ll use most.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Method 3: Split by a Multi-Character Delimiter

getline only splits on a single character. To split on a longer separator like "::" or ", ", use find and substr to walk through the string manually:

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

std::vector<std::string> split(const std::string& s, const std::string& delim) {
    std::vector<std::string> parts;
    size_t start = 0;
    size_t pos;
    while ((pos = s.find(delim, start)) != std::string::npos) {
        parts.push_back(s.substr(start, pos - start));
        start = pos + delim.length();   // jump past the delimiter
    }
    parts.push_back(s.substr(start));   // the final piece
    return parts;
}

int main() {
    for (const std::string& p : split("a::bb::ccc", "::"))
        std::cout << p << "\n";
    return 0;
}

find locates the next delimiter; substr grabs the text before it. We advance start by the full delimiter length so multi-character separators work correctly. The last substr after the loop captures the piece after the final delimiter.


Which Method to Use

Your delimiterBest method
Spaces / tabsstringstream >>
Single char (comma, ;)getline with delimiter
Multi-char string (::, ", ")find + substr

Start with the simplest method that fits your data. For most CSV-style and config parsing, the getline approach in Method 2 is all you need.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Sort Vector of Structs: By Field with std::sort
Next Post
C++ Check if String Contains Substring (find and contains)