I would like to be able to split a string into two parts, left
and right
, at the first occurance of separator
. For example, with #
as separator left#right#more
would result in left
and right#more
.
I have an method to do it:
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);
}
}
Now I have started using Boost and would like to compress this rather lengthy code to something more elegant. I know about boost::split()
, but that gives me three parts in the initial example (left
, right
and more
).
Any suggestions?