I want to separate string by character "," or ";".
std::string input = "abc,def;ghi";
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ',')) { //how to add here ";"?
std::cout << token << '\n';
}
I want to separate string by character "," or ";".
std::string input = "abc,def;ghi";
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ',')) { //how to add here ";"?
std::cout << token << '\n';
}
Use the Boost Tokenizer library:
boost::char_separator<char> sep(",;");
boost::tokenizer<boost::char_separator<char>> tokens(input, sep);
what about old way style?
std::string string = "abc,def;ghi";
std::vector<std::string>strings;
std::string temp;
for(int i=0; i < string.length(); i++)
{
if(string[i] == ',' || string[i] == ';')
{
strings.push_back(temp);
temp.clear();
}
else
{
temp += string[i];
}
}
strings.push_back(temp);
Using strsep in string.h from C library , you could do it like this:
std::string input = "abc,def;ghi";
const char* ss = intput.c_str();
const char token[] = ",;";
for (const char* part; (part = strsep(&ss, token)) != NULL;) {
std::cout<< part << std::endl;
}