I am using boost::tokenizer to get ';' separated fields from a string
.
I am able to retrieve the fields as shown in the code below but i have 2 questions:
- Is there any function which tokenizer provides to know the count of tokens in a string based on the separator provided?
- Supposing the test string has 3 fields
a;b;c
. The following piece of code will print all of them. But i need to print empty fields too. E.g. incase of a stringa;;;b;c
the token should also contain nothing as 2nd and 3rd element. Or in other words the 2nd and 3rd token should be empty.
#include <boost/tokenizer.hpp>
namespace std;
namespace boost;
int main()
{
string data="a;;;;b;c";
boost::char_separator<char> obj(";");
boost::tokenizer<boost::char_separator<char> > tokens(data,obj);
cout<<endl<<tokens.countTokens();
for(boost::tokenizer<boost::char_separator<char> >::iterator it=tokens.begin();
it!=tokens.end();
++it)
{
std::cout<<*it<<endl;
}
}