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
+9.4k Golang : Eroding and dilating image with OpenCV example
+18.9k Mac OSX : Homebrew and Golang
+12.5k Golang : Convert int(year) to time.Time type
+18.2k Golang : Write file with io.WriteString
+17.2k Golang : Clone with pointer and modify value
+14.3k Golang : How to get URL port?
+17.7k Golang : Put UTF8 text on OpenCV video capture image frame
+13.8k Golang : Fix image: unknown format error
+4.4k Linux : sudo yum updates not working
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+11.2k Golang : Generate DSA private, public key and PEM files example
+14.6k Golang : Submit web forms without browser by http.PostForm example