How to split string using istringstream with other delimiter than whitespace?
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::istringstream iss { "Cpp|is|fun" };
std::string s;
while ( std::getline( iss, s, '|' ) )
std::cout << s << std::endl;
return 0;
}
Demo
Generally speaking the istringstream approach is slow/inefficient and requires at least as much memory as the string itself (what happens when you have a very large string?). The C++ String Toolkit Library (StrTk) has the following solution to your problem:
#include <string>
#include <vector>
#include <deque>
#include "strtk.hpp"
int main()
{
std::string sentence1( "Cpp is fun" );
std::vector<std::string> vec;
strtk::parse(sentence1," ",vec);
std::string sentence2( "Cpp,is|fun" );
std::deque<std::string> deq;
strtk::parse(sentence2,"|,",deq);
return 0;
}
More examples can be found Here