Go 1.27 reference · Formatting, Strings and Text

Go strings.Split and strings.Join Function

Split a string into a slice around a separator, and join a slice of strings with a separator.

Go 1.27

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

func Split(s, sep string) []string   func Join(elems []string, sep string) string

Parameters

ParameterDescription
s, sep The input and the separator.
SplitN(s, sep, n) At most n parts.
Fields(s) Split on any whitespace.

Examples

Example 1
package main

import (
	"fmt"
	"strings"
)

func main() {
	parts := strings.Split("a,b,,c", ",")
	fmt.Printf("%q %d\n", parts, len(parts))
	fmt.Println(strings.Join([]string{"2026", "01", "15"}, "-"))
	fmt.Printf("%q\n", strings.Fields("  tea   and  cake "))
}

Output

["a" "b" "" "c"] 4
2026-01-15
["tea" "and" "cake"]

Run this on your computer — see “Get Started” for setup.

strings.Cut for key=value
package main

import (
	"fmt"
	"strings"
)

func main() {
	key, value, found := strings.Cut("timeout=30s", "=")
	fmt.Println(key, value, found)
	fmt.Printf("%q\n", strings.SplitN("a:b:c", ":", 2))
}

Output

timeout 30s true
["a" "b:c"]

Run this on your computer — see “Get Started” for setup.

Was this page helpful?