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".
import "strings" fmt.Println(strings.ToUpper("go"))
Try it
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.
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, butgo vetcan for some functions. - Byte offsets.
Indexreturns where the match starts in bytes. For non-ASCII text, use[]runeorutf8.RuneCountInStringif you need character positions. - Concatenating in a loop is quadratic. Use
strings.Builderorstrings.Join. - Prefer the newer helpers.
strings.Cutreplaces mostIndexplus slicing code,ReplaceAllis clearer thanReplace(..., -1), andCutPrefixreplacesHasPrefixfollowed byTrimPrefix. - Iterator functions (
SplitSeq,Linesand so on, added in Go 1.24) avoid allocating a slice when you only loop once. bytesmirrors this package for[]byte: almost every function has an identical sibling there.strings.Titleis deprecated because it mishandles punctuation; usegolang.org/x/text/casesfor real title casing.