0
0
GoHow-ToBeginner · 3 min read

How to Trim String in Go: Simple Guide with Examples

In Go, you can trim strings using functions from the strings package like strings.TrimSpace to remove spaces or strings.Trim to remove specific characters. These functions return a new string with the unwanted characters removed from the start and end.
📐

Syntax

The strings package provides several functions to trim strings:

  • strings.TrimSpace(s string) string: removes all leading and trailing white space characters.
  • strings.Trim(s string, cutset string) string: removes all leading and trailing characters contained in cutset.

Use these functions by passing your string as the first argument. They return a new trimmed string.

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "  hello world  "
	trimmed := strings.TrimSpace(s)
	customTrimmed := strings.Trim(s, " hd")

	fmt.Println("Original:", s)
	fmt.Println("TrimSpace:", trimmed)
	fmt.Println("Trim with cutset ' hd':", customTrimmed)
}
Output
Original: hello world TrimSpace: hello world Trim with cutset ' hd':ello worl
💻

Example

This example shows how to trim spaces and specific characters from a string using strings.TrimSpace and strings.Trim. It prints the original string and the trimmed results.

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	input := "\t Go programming! \n"

	// Remove leading and trailing white space
	trimmedSpace := strings.TrimSpace(input)

	// Remove specific characters '\t' and '\n'
	trimmedChars := strings.Trim(input, "\t\n")

	fmt.Println("Original:", input)
	fmt.Println("TrimSpace:", trimmedSpace)
	fmt.Println("Trim with '\t\n':", trimmedChars)
}
Output
Original: Go programming! TrimSpace: Go programming! Trim with '\t\n': Go programming!
⚠️

Common Pitfalls

One common mistake is expecting strings.Trim to remove substrings instead of individual characters. It removes any leading and trailing characters found in the cutset, not the exact substring.

Also, strings.TrimSpace only removes white space characters, not other characters.

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "xxhelloxx"

	// Wrong: expecting to remove 'xx' substring
	wrong := strings.Trim(text, "xx")

	// Right: removes all 'x' characters from start and end
	right := strings.Trim(text, "x")

	fmt.Println("Wrong trim result:", wrong)
	fmt.Println("Right trim result:", right)
}
Output
Wrong trim result: hello Right trim result: hello
📊

Quick Reference

Here is a quick summary of useful trimming functions in Go's strings package:

FunctionDescription
strings.TrimSpace(s string) stringRemoves all leading and trailing white space characters.
strings.Trim(s string, cutset string) stringRemoves all leading and trailing characters in cutset.
strings.TrimLeft(s string, cutset string) stringRemoves leading characters in cutset.
strings.TrimRight(s string, cutset string) stringRemoves trailing characters in cutset.

Key Takeaways

Use strings.TrimSpace to remove spaces and white space from both ends of a string.
Use strings.Trim with a cutset string to remove specific characters from the start and end.
strings.Trim removes any characters found in cutset individually, not substrings.
Always remember these functions return a new string; original strings are not changed.
For trimming only left or right side, use strings.TrimLeft or strings.TrimRight.