How to Split a String by a Delimiter in C++?
Last Updated :
19 May, 2025
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"
Similar Reads
How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks
2 min read
How to Declare Pointer to an Array of Strings in C++? In C++, an array of a string is used to store multiple strings in contiguous memory locations and is commonly used when working with collections of text data. In this article, we will learn how to declare a pointer to an array of strings in C++. Pointer to Array of String in C++If we are working wit
2 min read
How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
1 min read
How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to
2 min read
How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by
2 min read