Skip to content

proposal: strings, bytes: add CutLast #71151

@chipaca

Description

@chipaca

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions