Golang :Trim white spaces from a string
String manipulation functions are a must have for a programming language and Go has plenty of them. In this tutorial we will see how to trim white spaces of a string in Go
To trim white spaces from a string
str := " This is a string with white spaces\n "
fmt.Printf("%d %q\n", len(str), str)
trimmed := strings.TrimSpace(str)
fmt.Printf("%d %q\n", len(trimmed), trimmed)
output :
38 " This is a string with white spaces\n "
34 "This is a string with white spaces"
Full example code:
package main
import (
"fmt"
"strings"
)
func main() {
str := " This is a string with white spaces\n "
fmt.Printf("%d %q\n", len(str), str)
trimmed := strings.TrimSpace(str)
fmt.Printf("%d %q\n", len(trimmed), trimmed)
}
For more Strings Trim functions reference, please see
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.6k Golang : How to Set or Add Header http.ResponseWriter?
+8.2k Golang : Append and add item in slice
+29.6k Golang : How to create new XML file ?
+6.2k Golang : Create new color from command line parameters
+6.6k Elasticsearch : Shutdown a local node
+9.1k Golang : Go as a script or running go with shebang/hashbang style
+9.8k Golang : Find correlation coefficient example
+4.9k Which content-type(MIME type) to use for JSON data
+6.5k CodeIgniter : form input set_value cause " to become & quot
+19.5k Golang : Fix cannot download, $GOPATH not set error
+5k Nginx and PageSpeed build from source CentOS example
+6.7k Golang : When to use make or new?