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
Syntax
func Split(s, sep string) []string func Join(elems []string, sep string) string
Parameters
| Parameter | Description |
|---|---|
s, sep |
The input and the separator. |
SplitN(s, sep, n) |
At most n parts. |
Fields(s) |
Split on any whitespace. |
Examples
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.
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.
Related
strings.Contains, HasPrefix and Index· Check whether a string contains, starts or ends with a substring, and find its position.strings.Builder· Build a string efficiently piece by piece without creating a new string for every concatenation.- Go Strings · tutorial