## digitalmars.D.learn - A string splitting from the right

• bearophile (38/43) Jun 04 2013 If I have a string as "10,20,30" and I need to split it taking
```If I have a string as "10,20,30" and I need to split it taking
the part after the last comma and all before it, in Python I use
rsplit with optional argument 1:

s = "10,20,30"
a, b = s.rsplit(",", 1)
assert a, b == "10,20", "30"

The optional second argument tells Python when to stop splitting,
and the "r" suffix of split means from the right:

"10,20,30,40,50".rsplit(",", 2)

['10,20,30', '40', '50']
"10,20,30,40,50".split(",", 2)

['10', '20', '30,40,50']

One awkward way to do the same thing in D+Phobos is:

import std.algorithm: find;
import std.range: retro;
void main() {
auto s = "10,20,30";
string a = s.retro.find(",").retro[0 .. \$ - 1];
string b = s[a.length + 1 .. \$];
assert (a == "10,20");
assert(b == "30");
}

I don't see a rsplit or rSplit or rightSplit in std.string, but I
see findSplitAfter and findSplitBefore in std.algorithm:

s.findSplitAfter(",")
==>
Tuple!(string, string)("10,", "20,30")

s.findSplitBefore(",")
==>
Tuple!(string, string)("10", ",20,30")

If I use std.range.retro:
s.retro.findSplitBefore(",")
==>
Tuple!(Result, Result)(03, ,02,01)

Also notice the lack of " inside the tuple, those aren't strings.

And indeed this is not accepted:
s.retro.findSplitBefore(",")[0].retro

So do you think it's a good idea to add a rSplit(string,count=-1)
(or rightSplit) to std.string or std.array (and maybe add a
second argument to std.array.split), or I am missing something
from Phobos?

Bye and thank you,
bearophile
```
Jun 04 2013