Open In App

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

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.

Examples

Input: str = "geeks,for,geeks", delimiter = (,)

Output:
geeks
for
geeks

Explanation: Given string is splitted on the basis of comma (,) as delimiter.

Using Stringstream

We can split a string based on specific delimiter, first creating a stream to it using stringstream and then using getline() on this stream with custom delimiter to read the substring splitted by the delimiter. The getline() function will return nullptr to signal the end of the stream.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "geeks,for,geeks";

    // Create a stringstream object 
    // to str
    stringstream ss(str);
	
  	// Temporary object to store 
  	// the splitted string
    string t;
  
  	// Delimiter
    char del = ',';

   	// Splitting the str string 
   	// by delimiter
    while (getline(ss, t, del))
        cout << "\"" << t << "\"" << " ";
    return 0;
}

Output
"geeks" "for" "geeks" 

The above method is most commonly used method to split the string using a character as a separator/delimiter. But C++ also have other methods to do it:

Using strtok()

In C++, the strtok() function allows us to split a C-Style string into smaller parts (tokens) based on a specified delimiter. We can use it to split the whole string by repeatedly calling strtok() with the same delimiter, where it returns each substring one by one. At the end of the string, the strtok() method will return nullptr denoting that the whole string has been split.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char str[] = "geeks,for,geeks";

    // Delimiter
    const char *del = ",";

    // Splitting the string based on the
  	// delimiter
    char *t = strtok(str, del);

    // Continue extracting substring till
  	// strtok() does not returns nullptr
    while (t != nullptr) {
        cout << "\"" << t << "\"" << " ";

        // Get the next substring
        t = strtok(nullptr, del);
    }
    return 0;
}

Output
"geeks" "for" "geeks" 

This method works by modifying the original string, so it can be useful when we want to process each part of the string separately, such as when splitting a sentence into words or parsing data in a specific format.

Using find() and string::substr()

In C++, we can manually split a string by using the find() function to find the position of a delimiter and string::substr() to extract the substring before the delimiter. By repeatedly finding the delimiter and extracting substrings, we can split the entire string.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "geeks,for,geeks";

    // Delimiter
    string del = ",";

    // Find first occurrence of the delimiter
    auto pos = str.find(del);

    // While there are still delimiters in the
  	// string
    while (pos != string::npos) {

        // Extracting the substring up to the
      	// delimiter
        cout << "\"" << str.substr(0, pos) <<
          "\"" << " ";

        // Erase the extracted part from the
      	// original string
        str.erase(0, pos + del.length());

        // Find the next occurrence of the
      	// delimiter
        pos = str.find(del);
    }

    // Output the last substring (after the last
  	// delimiter)
    cout << "\"" << str << "\"" << " ";
    return 0;
}

Output
"geeks" "for" "geeks" 

Using regex

From C++11 onwards, the regex class allows us to define patterns for matching strings using regular expressions. We can use it to split a string based on a delimiter by defining a regex pattern that matches the delimiter. The sregex_token_iterator can then be used to iterate over the parts of the string between the delimiters.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "geeks,for,geeks";

    // Delimiter pattern
    regex del(",");

    // Create a regex_token_iterator 
    // to split the string
    sregex_token_iterator it(str.begin(), 
                     str.end(), del, -1);

    // End iterator for the 
    // regex_token_iterator
    sregex_token_iterator end;

    // Iterating through each token
    while (it != end) {
        cout << "\"" << *it << "\"" << " ";
        ++it;
    }
    return 0;
}

Output
"geeks" "for" "geeks" 

Next Article
Article Tags :
Practice Tags :

Similar Reads