我尝试在以下函数中使用split()
中提供的boost/algorithm/string.hpp
函数:
vector<std::string> splitString(string input, string pivot) { //Pivot: e.g., "##"
vector<string> splitInput; //Vector where the string is split and stored
split(splitInput,input,is_any_of(pivot),token_compress_on); //Split the string
return splitInput;
}
以下电话:
string hello = "Hieafds##addgaeg##adf#h";
vector<string> split = splitString(hello,"##"); //Split the string based on occurrences of "##"
将字符串拆分为"Hieafds" "addgaeg" "adf"
&amp; "h"
。但是,我不希望字符串被单个#
拆分。我认为问题出在is_any_of()
。
如何修改函数,以便仅按出现"##"
来分割字符串?
答案 0 :(得分:7)
你是对的,你必须使用 is_any_of()
std::string input = "some##text";
std::vector<std::string> output;
split( output, input, is_any_of( "##" ) );
<强>更新强>
但是,如果你想分成两个尖锐的,也许你必须使用正则表达式:
split_regex( output, input, regex( "##" ) );