Last updated:2026-10-12
Six Ways to Split Strings in C++¶
During development we often split a long string by a delimiter to get the strings we want. People say splitting strings in C++ is not that convenient, so today let's summarize 5 ways to split strings.
It covers styles from C++11 through C++20, plus the C-style approach.
To make the rest of the article easier to explain, let's first define a function prototype:
-
Parameters: two strings, one for the original string and one for the delimiter
-
Return value: an array of strings
Note: lazy version — you can get the complete code from the Knowledge Planet community.
1. Splitting with find¶
The first approach is the commonly used one: first locate the delimiter's position, then divide the string and add the pieces to an array. That gives us the following code.
nt start = 0;
int end = s.find(delim);
std::vector<std::string> result;
while (end != std::string::npos) {
result.push_back(s.substr(start, end - start));
start = end + delim.size();
end = s.find(delim, start);
}
result.push_back(s.substr(start, end - start));
Can this approach be optimized further?
The answer is yes. To make the code look more elegant, we can erase the substring already output each time and keep the same find logic.
while (end != std::string::npos) {
result.push_back(s.substr(0, end));
s.erase(0, end + delim.size());
end = s.find(delim);
}
2. Splitting with C++20 Ranges and Views¶
In C++20 we can use std::ranges::views::split() to create a view of the delimiter-separated parts, then use transform to convert each sub-view into a std::string.
auto split_transform_view = s | std::ranges::views::split(delim) |
std::ranges::views::transform([](auto&& str) {
return std::string(str.begin(), str.end());
});
Isn't that simple? If some of you didn't follow it, go back to the earlier C++20 articles.
3. Splitting with Regular Expressions¶
Use a std::regex object to build the delimiter pattern, then use std::sregex_token_iterator to iterate over the substrings separated by the regular expression pattern. -1 means extracting the non-matching strings, that is, everything other than the delimiters.
std::regex r(delim);
return {std::sregex_token_iterator(s.begin(), s.end(), r, -1),
std::sregex_token_iterator()};
4. Splitting with istringstream¶
getline can only pass one delimiter at a time, so after splitting you need to keep skipping the subsequent delimiters.
std::istringstream ss(s);
while (std::getline(ss, token, *(it = delim.begin()))) {
result.push_back(token);
it++;
while (it != delim.end() && ss.peek() == *(it)) {
it++;
ss.get();
}
}
5. C-Style Splitting with strtok¶
Use strtok() to split the string into tokens, then use a while loop to extract them one by one.
token = strtok((char*)s.c_str(), delim.c_str());
while (token != NULL) {
result.push_back(token);
token = strtok(NULL, delim.c_str());
}
These are the commonly used methods. Do you have other approaches? See you in the comments. For this section, the lazy version's complete code is available from the Knowledge Planet community.
Related Topics¶
- CPython Extension Development Notes | C++那些事
- libcurl C++ Development Notes | C++那些事
- How to Read Open Source C++ Code | C++那些事
- An Interesting Piece of C++ Code | C++那些事
- Printing 1-100 in C++ Without Loops | C++那些事