Skip to content

proposal: strings: add Lines #51039

@icholy

Description

@icholy

If I already have a string in memory, there's no simple and efficient way of splitting it into lines.

The naïve approach won't handle \r\n.

strings.Split(s, "\n")

Using a bufio.Scanner works but it creates copies instead of slicing the existing string.

var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
    lines = append(lines, sc.Text())
}

I'd like a new strings.Lines function similar to strings.Fields for this use-case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions