Separating Comma-Delimited Strings Using Stringstream
In the provided problem, the task is to separate a comma-delimited string into individual tokens. While the stringstream::operator can effortlessly separate words by spaces, it falls short when it comes to commas.
To overcome this challenge, we employ a modified approach:
#include <iostream> #include <sstream> int main() { std::string input = "abc,def,ghi"; std::istringstream ss(input); std::string token; // Use getline to separate by commas while (std::getline(ss, token, ',')) { std::cout << token << '\n'; } return 0; }
In this modified code:
The output accurately separates the input string into individual tokens:
abc def ghi
The above is the detailed content of How Can I Efficiently Separate Comma-Delimited Strings in C ?. For more information, please follow other related articles on the PHP Chinese website!