background
there does not appear to be a turnkey way to do a SplitN of a string starting at the tail and proceeding toward the head. similarly, the following do not appear to exist:
SplitAfter, but before (put the separator on the next element: []string{"f" ",o" ",o"})
SplitAfterN, but before
Replace but start from the tail
proposal
add the following symbols to the strings package:
func SplitLastN(s, sep string, n int) []string
func SplitBefore(s, sep string) []string
func SplitBeforeN(s, sep string, n int) []string
func ReplaceLast(s, sep string, n int) []string
alternatives
we could say that these are weird and esoteric enough to not justify symbols for, but at least that decision is documented. there are workarounds to get this behaviour if you really need it:
rest, last := s[strings.LastIndex(s, ","):] // SplitLast(s, ",", 1)
I am very flexible on the names; so LastSplitN, ReplaceReverseN, or anything else is fine; I am just interested in the functionality.
background
there does not appear to be a turnkey way to do a SplitN of a string starting at the tail and proceeding toward the head. similarly, the following do not appear to exist:
SplitAfter, but before (put the separator on the next element:[]string{"f" ",o" ",o"})SplitAfterN, but beforeReplacebut start from the tailproposal
add the following symbols to the
stringspackage:alternatives
we could say that these are weird and esoteric enough to not justify symbols for, but at least that decision is documented. there are workarounds to get this behaviour if you really need it:
I am very flexible on the names; so
LastSplitN,ReplaceReverseN, or anything else is fine; I am just interested in the functionality.