我希望能够将一个字符串分为两部分,left
和right
,首次出现separator
。例如,使用#
作为分隔符left#right#more
将导致left
和right#more
。使用C++ Boost将字符串拆分为两部分?
我必须这样做的方法:
void misc::split(const string &input, string &left, string &right, char separator)
{
int index = input.find(separator);
if (index == string::npos)
{
left = input;
right.erase();
}
else
{
right = input.substr(index + 1);
left = input.substr(0, index);
}
}
现在我已经使用Boost开始,希望这个相当冗长的代码压缩的东西更优雅。我知道boost::split()
,但在初始示例中给出了三个部分(left
,right
和more
)。
有什么建议吗?
您可能会受到[提案](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3510.html)和[this](http:// stackoverflow.com/questions/5734304/c-boost-split-string)。 –