In this tutorial, we will explore how to split strings in C++ using delimiters. A delimiter is a character or string that marks the boundary between separate elements in a string. We will cover both single-character and multi-character delimiters.
Introduction to String Splitting
String splitting is a common operation in programming where you need to divide a string into smaller substrings based on a specific delimiter. For example, if you have a string "hello,world,cpp" and want to split it using the comma (",") as the delimiter, you would get three separate strings: "hello", "world", and "cpp".
Single-Character Delimiters
To split a string using a single-character delimiter in C++, you can use the std::getline
function along with an std::stringstream
. The std::getline
function reads from an input stream until it encounters the specified delimiter.
Here’s an example code snippet that demonstrates how to split a string using a single-character delimiter:
#include <iostream>
#include <sstream>
#include <vector>
// Function to split a string based on a single character delimiter
std::vector<std::string> split(const std::string& s, char delim) {
std::vector<std::string> result;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
result.push_back(item);
}
return result;
}
int main() {
std::string str = "adsf+qwer+poui+fdgh";
char delimiter = '+';
std::vector<std::string> v = split(str, delimiter);
for (const auto& i : v) {
std::cout << i << std::endl;
}
return 0;
}
Multi-Character Delimiters
For multi-character delimiters, we can use the std::string::find
function to locate the position of the delimiter in the string. Once found, we use std::string::substr
to extract the token.
Here’s an example code snippet that demonstrates how to split a string using a multi-character delimiter:
#include <iostream>
#include <vector>
// Function to split a string based on a multi-character delimiter
std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
int main() {
std::string str = "adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih";
std::string delimiter = "-+";
std::vector<std::string> v = split(str, delimiter);
for (const auto& i : v) {
std::cout << i << std::endl;
}
return 0;
}
Using C++20 Ranges
If you’re using C++20 or later, you can utilize the std::ranges
library to split strings in a more concise and expressive way. Here’s an example:
#include <iostream>
#include <ranges>
#include <string_view>
int main() {
std::string hello = "text to be parsed";
auto split = hello
| std::views::split(' ')
| std::views::transform([](auto&& str) { return std::string_view(&*str.begin(), std::ranges::distance(str)); });
for (const auto& word : split) {
std::cout << word << std::endl;
}
}
Conclusion
In this tutorial, we’ve covered the basics of string splitting in C++ using both single-character and multi-character delimiters. We’ve also explored how to use C++20 ranges for a more modern approach. By mastering these techniques, you’ll be able to efficiently process strings in your C++ applications.