Skip to content

String functions

The strings package provides many utility functions for string manipulation.

Common functions: Contains, Count, HasPrefix, HasSuffix, Index, Join, Repeat, Replace, Split, ToLower, ToUpper, TrimSpace.

Use several string functions.

package main
import (
"fmt"
"strings"
)
func main() {
s := " Hello, Go! "
fmt.Println("Trimmed:", strings.TrimSpace(s))
fmt.Println("Upper:", strings.ToUpper(s))
fmt.Println("Contains 'Go':", strings.Contains(s, "Go"))
fmt.Println("Split by space:", strings.Split(s, " "))
fmt.Println("Replace Go with Gophers:", strings.ReplaceAll(s, "Go", "Gophers"))
fmt.Println("Join:", strings.Join([]string{"a", "b", "c"}, "-"))
}
Terminal window
Trimmed: Hello, Go!
Upper: HELLO, GO!
Contains 'Go': true
Split by space: [ Hello, Go! ]
Replace Go with Gophers: Hello, Gophers!
Join: a-b-c