Go: How to trim leading and trailing whitespace from a string

Use the strings.TrimSpace function to remove leading and trailing whitespace (as defined by Unicode):

s := strings.TrimSpace("\t Hello world!\n ")
fmt.Printf("%q", s)
// Output: "Hello world!"

To remove other leading and trailing characters, use strings.Trim. To remove only the leading or the trailing characters, use strings.TrimLeft or strings.TrimRight.

Comments

Be the first to comment!