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
+44.4k Golang : Use wildcard patterns with filepath.Glob() example
+11.4k Golang : Surveillance with web camera and OpenCV
+29k Golang : JQuery AJAX post data to server and send data back to client example
+15.2k Golang : rune literal not terminated error
+9.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+33.3k Golang : All update packages with go get command
+36k Golang : Validate IP address
+30.1k Get client IP Address in Go
+15.1k Golang : Delete certain files in a directory
+8k Swift : Convert (cast) Character to Integer?
+5.7k AWS S3 : Prevent Hotlinking policy
+6.7k Golang : How to call function inside template with template.FuncMap