Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท strings and strconv

The Go strings package

Overview of Go's strings package: searching, splitting, joining, trimming, replacing, case conversion, Builder, Reader and Replacer, with live examples.

The strings package holds the everyday functions for working with UTF-8 text: finding, splitting, joining, trimming, replacing and changing case. It also provides three types: Builder for efficient string construction, Reader for reading a string as an io.Reader, and Replacer for many replacements in one pass.

Go strings are immutable byte sequences, so every function here returns a new string (often a substring that shares memory with the original) and never changes its argument. Import it with import "strings".

Go
import "strings"

fmt.Println(strings.ToUpper("go"))

Try it

Go A tour of the package

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Builder, Reader and Replacer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Functions are grouped by purpose. Each has its own page where marked.

Task Functions
Search Contains, ContainsAny, ContainsRune, Index, LastIndex, IndexByte, IndexAny, IndexFunc, Count
Prefix and suffix HasPrefix, HasSuffix, CutPrefix, CutSuffix
Split Split, SplitN, SplitAfter, Fields, FieldsFunc, Cut
Join and repeat Join, Repeat
Replace Replace, ReplaceAll, Replacer
Trim Trim, TrimSpace, TrimLeft, TrimRight, TrimPrefix, TrimSuffix, TrimFunc
Case ToUpper, ToLower, ToTitle, EqualFold
Types Builder, Reader, Replacer
Iterators (Go 1.24+) Lines, SplitSeq, SplitAfterSeq, FieldsSeq, FieldsFuncSeq
Other Compare, Map, ToValidUTF8, Clone

Edge cases that apply everywhere: an empty separator or pattern usually matches at every rune boundary (Split(s, "") splits into UTF-8 characters); index results are byte offsets, not rune counts; functions never return an error.

Usage notes

  • Strings are immutable. Calling strings.ToUpper(s) without using the result does nothing. The compiler will not warn you, but go vet can for some functions.
  • Byte offsets. Index returns where the match starts in bytes. For non-ASCII text, use []rune or utf8.RuneCountInString if you need character positions.
  • Concatenating in a loop is quadratic. Use strings.Builder or strings.Join.
  • Prefer the newer helpers. strings.Cut replaces most Index plus slicing code, ReplaceAll is clearer than Replace(..., -1), and CutPrefix replaces HasPrefix followed by TrimPrefix.
  • Iterator functions (SplitSeq, Lines and so on, added in Go 1.24) avoid allocating a slice when you only loop once.
  • bytes mirrors this package for []byte: almost every function has an identical sibling there.
  • strings.Title is deprecated because it mishandles punctuation; use golang.org/x/text/cases for real title casing.