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
| Name | Signature / Value | Description |
|---|---|---|
| strings.Contains | func Contains(s, substr string) bool | Report if substr is within s |
| strings.ContainsAny | func ContainsAny(s, chars string) bool | Report if any Unicode code point in chars is in s |
| strings.Count | func Count(s, substr string) int | Count non-overlapping instances of substr in s |
| strings.HasPrefix | func HasPrefix(s, prefix string) bool | Report if s begins with prefix |
| strings.HasSuffix | func HasSuffix(s, suffix string) bool | Report if s ends with suffix |
| strings.Index | func Index(s, substr string) int | Return index of first instance of substr in s |
| strings.LastIndex | func LastIndex(s, substr string) int | Return index of last instance of substr in s |
| strings.Replace | func Replace(s, old, new string, n int) string | Replace first n occurrences of old with new |
| strings.ReplaceAll | func ReplaceAll(s, old, new string) string | Replace all occurrences of old with new |
| strings.Split | func Split(s, sep string) []string | Split s into substrings separated by sep |
| strings.SplitN | func SplitN(s, sep string, n int) []string | Split into at most n substrings |
| strings.Join | func Join(elems []string, sep string) string | Join elements with separator |
| strings.Fields | func Fields(s string) []string | Split on whitespace, discarding empty strings |
| strings.TrimSpace | func TrimSpace(s string) string | Trim leading and trailing whitespace |
| strings.Trim | func Trim(s, cutset string) string | Trim leading and trailing chars in cutset |
| strings.TrimLeft | func TrimLeft(s, cutset string) string | Trim leading chars in cutset |
| strings.TrimRight | func TrimRight(s, cutset string) string | Trim trailing chars in cutset |
| strings.TrimPrefix | func TrimPrefix(s, prefix string) string | Remove prefix from s if present |
| strings.TrimSuffix | func TrimSuffix(s, suffix string) string | Remove suffix from s if present |
| strings.ToUpper | func ToUpper(s string) string | Return uppercase copy of s |
| strings.ToLower | func ToLower(s string) string | Return lowercase copy of s |
| strings.Title | func Title(s string) string | Return title-cased copy of s |
| strings.EqualFold | func EqualFold(s, t string) bool | Case-insensitive comparison |
| strings.Repeat | func Repeat(s string, count int) string | Return count copies of s concatenated |
| strings.NewReader | func NewReader(s string) *Reader | Return a Reader that reads from s |
| strings.NewReplacer | func NewReplacer(oldnew ...string) *Replacer | Create multi-pair string replacer |
| strings.Builder | type Builder struct{} | Efficient string builder (use WriteString + String()) |
| strings.Cut | func Cut(s, sep string) (before, after string, found bool) | Cut s around first instance of sep (Go 1.18+) |
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