Proposal Details
A couple of times now while chopping up some strings I've happily enjoyed strings.Cut but had to write my own CutLast (the latest was implementing a subset of the package-url spec where some of the things you need to find from the back of the string, and some from the front). It's a silly thing but it feels like an oversight.
For reference, here is one of the CutLast I've implemented, verbatim. Trivial and obvious:
// CutLast slices s around the last instance of sep,
// returning the text before and after sep.
// The found result reports whether sep appears in s.
// If sep does not appear in s, CutLast returns s, "", false.
func stringsꞏCutLast(s, sep string) (before, after string, found bool) {
if i := strings.LastIndex(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
Proposal Details
A couple of times now while chopping up some strings I've happily enjoyed
strings.Cutbut had to write my ownCutLast(the latest was implementing a subset of thepackage-urlspec where some of the things you need to find from the back of the string, and some from the front). It's a silly thing but it feels like an oversight.For reference, here is one of the
CutLastI've implemented, verbatim. Trivial and obvious: