Skip to content

proposal: strings: add First/Last function #63197

Description

@xrfang

I encountered many times the following scenario:

func First(s string, n int) string {
    if len(s) <= n {
        return s
    }
    return s[:n]
}

This simple function try to get at most N-bytes from a string. Likewise, for at most N-bytes "tail" from a string:

func Last(s string, n int) string {
    if len(s) <= n {
        return s
    }
    return s[len(s)-n:]
}

These functions will many some programs easier to read, like the TrimPrefix/TrimSuffix functions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions