Go Reference ยท strings and strconv
Go strings.Trim() family of functions
Reference for Go strings.Trim, TrimLeft, TrimRight, TrimSpace, TrimPrefix, TrimSuffix and TrimFunc, including the cutset pitfall that trips up many beginners.
The Trim family removes unwanted characters from the ends of a string. TrimSpace strips white space; Trim, TrimLeft and TrimRight strip any characters from a set you give; TrimPrefix and TrimSuffix remove one exact piece of text; TrimFunc removes runes for which a function returns true.
The most common surprise is that Trim's second argument is a cutset (a bag of characters), not a prefix or suffix string.
s := strings.TrimSpace(" hello \n") // "hello"
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
func TrimSpace(s string) string func Trim(s, cutset string) string func TrimLeft(s, cutset string) string func TrimRight(s, cutset string) string func TrimPrefix(s, prefix string) string func TrimSuffix(s, suffix string) string func TrimFunc(s string, f func(rune) bool) string func TrimLeftFunc(s string, f func(rune) bool) string func TrimRightFunc(s string, f func(rune) bool) string
| Name | Description |
|---|---|
cutset |
Every rune in this string is removed from the relevant end(s), repeatedly, until a rune not in the set is reached. |
prefix, suffix |
Exact text removed at most once. If it is not present, s is returned unchanged. |
f |
Predicate on runes; trimming continues while it returns true. |
Returns: a substring of s (no copy is made; it shares memory with s). If everything is trimmed the result is "".
Edge cases: an empty cutset trims nothing. TrimSpace removes all Unicode white space (including U+00A0 and newlines) from both ends, but never from the middle. Middle characters are never touched by any Trim function.
Usage notes
TrimLeft("abcabcX", "abc")removes every leading a, b or c and leavesX. To remove one exact prefix, useTrimPrefix. This confusion is the most common bug with this family.- Use
CutPrefixandCutSuffix(Go 1.20+) when you need to know whether something was removed. - Trimming inside a string needs
strings.ReplaceAll,strings.FieldsplusJoin, or a regexp. - Windows line endings:
strings.TrimRight(s, "\r\n")removes any number of trailing CR/LF characters;TrimSuffix(s, "\n")removes just one. - Result shares memory. A short trimmed result keeps the original large string alive; use
strings.Cloneif that matters. - Idempotent: trimming twice gives the same result as trimming once.
- Bytes:
bytes.Trim*mirrors these functions for[]byte.