How to trim leading and trailing white spaces of a string in Golang?
Removing leading and trailing spaces from a string eliminates all whitespace before the first character and after the last character in the string. For example, removing leading and trailing spaces from " abc def " results in "abc def". The strings.TrimSpace() method from the standard library, trim the white spaces from both ends of a string.
Example
package main
import (
"fmt"
"strings"
)
func main() {
str := "\t Hello, World\n "
fmt.Printf("Before Trim Length: %d String:%v\n", len(str), str)
trim := strings.TrimSpace(str)
fmt.Printf("After Trim Length: %d String:%v\n", len(trim), trim)
}
Most Helpful This Week
How to check if a string contains certain characters in Golang?
How to check if a string contains a white space in Golang?
How to check if a string contains a substring in Golang?
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
How to check string contains uppercase lowercase character in Golang?
How to check lowercase characters in a string in Golang?
Strip all white spaces, tabs, newlines from a string
How to check if a string contains only letters in Golang?
Golang String Concatenation
How to check if a string contains a numbers in Golang?
How to check UPPERCASE characters in a string in Golang?