Skip to content

proposal: strings: add First/Last function #63197

@xrfang

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions