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
+7.8k Golang : Simple histogram example
+4k Which content-type(MIME type) to use for JSON data
+8k Golang : Get SPF and DMARC from email headers to fight spam
+5k Golang : List all packages and search for certain package
+16.2k Golang : Iterate linked list example
+8.5k Golang : How to generate Code 39 barcode?
+12.4k Golang : Strings comparison
+6.4k Golang : Not able to grep log.Println() output
+16.3k Golang : Clone with pointer and modify value
+34.4k Golang : Validate IP address
+4.9k Cash Flow : 50 days to pay your credit card debt
+12.9k Golang : How to pass map to html template and access the map's elements