HOWTO · C++

How to Split a Comma-Separated String in C++

Split a simple comma-separated string in C++ with std::stringstream and std::getline, or with std::string::find and std::string::substr.

On this page

For a simple comma-delimited string, std::stringstream with std::getline is a straightforward way to collect each value in a std::vector<std::string>. If you need to control how each piece is extracted, std::string::find and std::string::substr provide a manual alternative. These approaches split on a delimiter; they do not parse full CSV syntax, such as commas inside quoted fields.

Split a comma-separated string with std::stringstream and std::getline

Include <sstream> to construct a stream from the input string, and <vector> to store the pieces. Pass the comma as the third argument to std::getline; each call reads up to that delimiter and places the extracted text in item. The loop adds each item to the vector, then prints the collected values.

Use std::vector<std::string> when later code needs to index the tokens or iterate over them again; it stores each extracted value as a separate string in input order.

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

int main() {
    const std::string input = "apple,banana,cherry,dates";
    std::stringstream stream(input);
    std::vector<std::string> items;
    std::string item;

    while (std::getline(stream, item, ',')) {
        items.push_back(item);
    }

    for (const std::string& value : items) {
        std::cout << value << '\n';
    }
}
apple
banana
cherry
dates

This example uses a comma as the delimiter and stores the four extracted strings in order. std::getline with a delimiter can produce an empty item between consecutive commas, while a delimiter at the very end does not produce a final item in this loop. If your input needs different treatment for such cases, decide what each delimiter position should mean and account for it in the parsing logic. The extracted strings keep any spaces that are part of the input, so trim each item separately if surrounding whitespace should be discarded. Change the delimiter argument when the source uses another single character, such as a semicolon.

Split a comma-separated string with std::string::find and std::string::substr

The std::string::find member locates the next comma, and std::string::substr copies the characters before it. Keep a starting index into the original input instead of erasing characters as you go. When find returns std::string::npos, there are no more commas, so take the remaining suffix as the final item and stop.

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

int main() {
    const std::string input = "apple,banana,cherry,dates";
    std::vector<std::string> items;
    std::string::size_type start = 0;

    while (true) {
        const std::string::size_type comma = input.find(',', start);
        if (comma == std::string::npos) {
            items.push_back(input.substr(start));
            break;
        }

        items.push_back(input.substr(start, comma - start));
        start = comma + 1;
    }

    for (const std::string& value : items) {
        std::cout << value << '\n';
    }
}
apple
banana
cherry
dates

The final substr(start) call is important: it adds the text after the last comma, including the last value when there are no more delimiters. This version also treats an input with a trailing comma as having a final empty item, unlike the stream loop above. For a wholly empty input it likewise stores one empty string, so adjust the loop condition if your application should treat that input as no values. The search starts at the character after each comma, which avoids changing the input while parsing and makes it easy to retain the original string for later use. Neither approach handles quoted commas or escaped quotes; use a CSV parser when those rules are part of the input format.