繁体   English   中英

如何拆分字符串并使用boost :: split保留分隔符?

[英]How to split a string and keep the delimiters using boost::split?

我有一个像这样的字符串:

std::string input("I #am going to# learn how #to use #boost# library#");

我这样做:

std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));

得到了这个:(splitVector)

splitVector:
        "I "
        "am going to"
        " learn how " 
        "to use "
        "boos"
        " library"
        "" // **That's odd, why do I have an empty string here ?**

但需要这样的东西:

splitVector:
    "I "
    "#am going to"
    "# learn how "
    "#to use "
    "#boost"
    "# library"
    "#"

怎么做 ? 或者也许在boost库中有另一种方法可以做到这一点? 为什么我在splitVector得到一个空字符串?

你不能使用boost::split因为使用boost/algorithm/string/find_iterator.hppsplit_iterator的内部实现吞下了令牌。

但是你可以使用boost::tokenizer ,因为它有一个保留分隔符的选项:

每当在输入序列中看到分隔符时,当前令牌就完成了,并且新的令牌开始。 dropped_delims中的分隔符不会在输出中显示为令牌,而keep_delims中的分隔符确实显示为令牌。
http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm

见下一个直播:

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

int main() {
    // added consecutive tokens for illustration
    std::string text = "I #am going to# learn how ####to use #boost# library#";    
    boost::char_separator<char> sep("", "#"); // specify only the kept separators
    boost::tokenizer<boost::char_separator<char>> tokens(text, sep);
    for (std::string t : tokens) { std::cout << "[" << t << "]" << std::endl; }
}
/* Output:
[I ]
[#]
[am going to]
[#]
[ learn how ]
[#]
[#]
[#]
[#]
[to use ]
[#]
[boost]
[#]
[ library]
[#] */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM