Standard library / strings

strings.SplitAfterN()

Splits after separators into at most n pieces; n=0 returns nil and n<0 is unlimited. Expected stdout: '[a,|b,c]\n'.

Formal Syntax & Structure

go syntaxgo
func SplitAfterN(s, sep string, n int) []string

Parameters & Attributes

NameTypeDescription
sstringInput text or sequence.
sepstringSeparator or search sequence.
nintMaximum result count or number of replacements, as specified.

Return Value

[]string

Splits after separators into at most n pieces; n=0 returns nil and n<0 is unlimited.

Working Example

Example Snippetgo
package main
import (  "fmt"  "strings")
func main() {  v := strings.SplitAfterN("a,b,c", ",", 2)  fmt.Print("[")  for i, x := range v {    if i > 0 {      fmt.Print("|")    }    fmt.Print(string(x))  }  fmt.Println("]")}
Go Reference