Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Go Index

🔤 strings — String manipulation functions

Provides functions for searching, splitting, replacing, and transforming strings. In Go, strings are immutable UTF-8 byte sequences. This package covers all common string operations without needing external libraries.

Import: import "strings" | Docs: pkg.go.dev/strings

📋 Functions, Types & Constants

NameSignature / ValueDescription
strings.Containsfunc Contains(s, substr string) boolReport if substr is within s
strings.ContainsAnyfunc ContainsAny(s, chars string) boolReport if any Unicode code point in chars is in s
strings.Countfunc Count(s, substr string) intCount non-overlapping instances of substr in s
strings.HasPrefixfunc HasPrefix(s, prefix string) boolReport if s begins with prefix
strings.HasSuffixfunc HasSuffix(s, suffix string) boolReport if s ends with suffix
strings.Indexfunc Index(s, substr string) intReturn index of first instance of substr in s
strings.LastIndexfunc LastIndex(s, substr string) intReturn index of last instance of substr in s
strings.Replacefunc Replace(s, old, new string, n int) stringReplace first n occurrences of old with new
strings.ReplaceAllfunc ReplaceAll(s, old, new string) stringReplace all occurrences of old with new
strings.Splitfunc Split(s, sep string) []stringSplit s into substrings separated by sep
strings.SplitNfunc SplitN(s, sep string, n int) []stringSplit into at most n substrings
strings.Joinfunc Join(elems []string, sep string) stringJoin elements with separator
strings.Fieldsfunc Fields(s string) []stringSplit on whitespace, discarding empty strings
strings.TrimSpacefunc TrimSpace(s string) stringTrim leading and trailing whitespace
strings.Trimfunc Trim(s, cutset string) stringTrim leading and trailing chars in cutset
strings.TrimLeftfunc TrimLeft(s, cutset string) stringTrim leading chars in cutset
strings.TrimRightfunc TrimRight(s, cutset string) stringTrim trailing chars in cutset
strings.TrimPrefixfunc TrimPrefix(s, prefix string) stringRemove prefix from s if present
strings.TrimSuffixfunc TrimSuffix(s, suffix string) stringRemove suffix from s if present
strings.ToUpperfunc ToUpper(s string) stringReturn uppercase copy of s
strings.ToLowerfunc ToLower(s string) stringReturn lowercase copy of s
strings.Titlefunc Title(s string) stringReturn title-cased copy of s
strings.EqualFoldfunc EqualFold(s, t string) boolCase-insensitive comparison
strings.Repeatfunc Repeat(s string, count int) stringReturn count copies of s concatenated
strings.NewReaderfunc NewReader(s string) *ReaderReturn a Reader that reads from s
strings.NewReplacerfunc NewReplacer(oldnew ...string) *ReplacerCreate multi-pair string replacer
strings.Buildertype Builder struct{}Efficient string builder (use WriteString + String())
strings.Cutfunc Cut(s, sep string) (before, after string, found bool)Cut s around first instance of sep (Go 1.18+)

💡 Example Program

Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "  Hello, Go World!  "

    fmt.Println(strings.TrimSpace(s))
    fmt.Println(strings.ToUpper(s))
    fmt.Println(strings.ToLower(s))
    fmt.Println(strings.Contains(s, "Go"))
    fmt.Println(strings.HasPrefix(strings.TrimSpace(s), "Hello"))
    fmt.Println(strings.Count(s, "l"))
    fmt.Println(strings.ReplaceAll(s, "Go", "Golang"))
    fmt.Println(strings.Index(s, "Go"))

    // Split and Join
    csv := "apple,banana,cherry,date"
    parts := strings.Split(csv, ",")
    fmt.Println(parts)
    fmt.Println(strings.Join(parts, " | "))

    // Fields — split on any whitespace
    words := strings.Fields("  foo   bar  baz  ")
    fmt.Println(words)

    // Cut — split around separator (Go 1.18+)
    before, after, found := strings.Cut("user@example.com", "@")
    fmt.Printf("before=%s  after=%s  found=%v\n", before, after, found)

    // Builder — efficient string construction
    var b strings.Builder
    for i := 0; i < 5; i++ {
        fmt.Fprintf(&b, "item%d ", i)
    }
    fmt.Println(b.String())

    // NewReplacer — multiple substitutions in one pass
    r := strings.NewReplacer("Go", "Golang", "World", "Universe")
    fmt.Println(r.Replace("Hello, Go World!"))
}
// go run main.go

← io  |  🏠 Index  |  strconv →