【发布时间】:2015-08-28 12:00:01
【问题描述】:
我希望能够在第一次出现separator 时将字符串分成两部分,left 和right。例如,使用# 作为分隔符left#right#more 将导致left 和right#more。
我有办法做到这一点:
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)。
有什么建议吗?
【问题讨论】: