boost::split leaves empty tokens at the beginning and end of string - is this desired behaviour?

前端 未结 3 729
暗喜
暗喜 2021-01-02 06:32

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         


        
相关标签:
3条回答
  • 2021-01-02 07:00

    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.

    0 讨论(0)
  • 2021-01-02 07:03

    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.

    0 讨论(0)
  • 2021-01-02 07:03

    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
    
    0 讨论(0)
提交回复
热议问题