Parsing a Comma Delimited std::string in C++



In this article, we will discuss all the approaches to parse a string delimited by comma using a C/C++ program. First of all, let's understand the problem statement.

The input of this problem is a string containing multiple words that are seperated by commas. Our task is to print each word space seperated to the output console. For example:

// Input String
"Hello, World, From, 2025"

// Output
"Hello" "World" "From" "2025"

Parse Comma Delimited String

Here is the list of approaches to parse comma delimited string to words using c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.

Using Stringstream to Parse Comma Delimited String

The stringstream is an object of string class defined in the <sstream> header file. It is used for reading from the stream strings. In this method, first we will pass the given string to a stringstream, then we use getline() function to search for the comma ',' token inside the stream. For each comma encoutered, the previous word will be added in a vector string. Below example shows how to implement this method.

Example

The code below uses stringstream object to extract words from string.

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
    string input = "Banana,Apple,Orange";
    stringstream ss(input);
    string token;
    vector<string> result;

    while (getline(ss, token, ',')) {
        result.push_back(token);
    }

    for (const auto& word : result) cout << word << " "; 
}

The output of the above code will be:

Banana Apple Orange

Using find() Function to Parse Comma Delimited String

The find() function is a member function of the string class in C++. It returns the position of the first occurrence of a substring in a string. This approach gives you full control over algorithm and you can modify it.

Example

In code below, we used the find() function to repeatedly find comma and extract substring based on that.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    string input = "one,two,three";
    vector<string> result;
    size_t start = 0, end;

    while ((end = input.find(',', start)) != string::npos) {
        result.push_back(input.substr(start, end - start));
        start = end + 1;
    }
    result.push_back(input.substr(start));  // last part

    for (const auto& word : result) cout << word << " ";  
}

The output of the above code will be:

one two three

Using Regular Expression to Parse Comma Delimited String

The regex_search() function is a member function of the regex library in C++. It performs a regular expression search and returns a boolean value indicating whether a match was found or not. Let's see an example of how to use it.

Example

In the code below, we used the regex_search() function to find comma inside the given string, and extracted words out of it.

#include <iostream>
#include <regex>
#include <vector>
using namespace std;

int main() {
    string input = "Hello,World,2025";
    regex re(",");
    sregex_token_iterator it(input.begin(), input.end(), re, -1);
    sregex_token_iterator end;

    vector<string> result(it, end);
    for (const auto& word : result) cout << word << " ";  
}

The output of the above code will be:

Hello World 2025
Updated on: 2025-04-23T18:47:30+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements