3 ways to trim whitespace (or other characters) from a string
yourbasic.org/golang
Use the strings.TrimSpace
function
to remove leading and trailing whitespace as defined by Unicode.
s := strings.TrimSpace("\t Goodbye hair!\n ")
fmt.Printf("%q", s) // "Goodbye hair!"
- To remove other leading and trailing characters, use
strings.Trim
. - To remove only the leading or the trailing characters, use
strings.TrimLeft
orstrings.TrimRight
.