Skip to content

proposal: strings: missing mirror functions #46514

@colin-sitehost

Description

@colin-sitehost

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions