Skip to main content
The standard library’s strings package provides many useful string-related functions. Here are some examples to give you a sense of the package.

Common String Operations

These are functions from the package, not methods on the string object itself, so you need to pass the string as the first argument:
import (
    "fmt"
    s "strings"
)

var p = fmt.Println

String Testing

p("Contains:  ", s.Contains("test", "es"))
// Output: Contains:   true

String Searching

p("Count:     ", s.Count("test", "t"))
// Output: Count:      2

p("Index:     ", s.Index("test", "e"))
// Output: Index:      1

String Manipulation

p("Join:      ", s.Join([]string{"a", "b"}, "-"))
// Output: Join:       a-b

Case Conversion

p("ToLower:   ", s.ToLower("TEST"))
// Output: ToLower:    test

p("ToUpper:   ", s.ToUpper("test"))
// Output: ToUpper:    TEST

String Formatting

Learn about string formatting with printf-style verbs

Package Reference

Find more functions in the strings package docs.

Build docs developers (and LLMs) love