Since I couldn\'t find anything on this in the documentation, I thought I ask it here. I have the following program (C++11):
#include
#inc
The behavior is intentional, because you could recreate the string (complete with starting and trailing spaces) from the split version. Boost doesn't know if that whitespace is significant or not to you (it might be, as some file formats, for example, might force leading spaces/specific space counts).
You should trim_if
or trim
as you are if you do need to remove leading/trailing spaces.
If eCompress
argument is set to token_compress_on
, adjacent separators are merged together. Otherwise, every two separators delimit a token.
Here
It does not remove the tokens only merges them.
boost::split
always returns n + 1
tokens where n
is number of separators in the input string. So don't be surprised when it returns 1 token when you pass it an empty string.
The rationale behind it is quite simple. Imagine that you are parsing a CSV file. You need to get the exactly same number of elements regardless whether the last token is empty or not.
It is much easier to remove the empty tokens than to guess whether they were supposed to be in the result or not. Credit
This behaviour is similar to python
>>> len("".split(','))
1