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.5k Golang : How to get ECDSA curve and parameters data?
+7.7k Facebook : Getting the friends list with PHP return JSON format
+12k Golang : rune literal not terminated error
+7.4k Golang : Populate or initialize struct with values example
+30.8k Golang : All update packages with go get command
+8.6k Golang : Concatenate (combine) buffer data example
+17k Golang : Archive directory with tar and gzip
+10.6k Python : Convert IPv6 address to decimal and back to IPv6
+29.3k Golang : Validate email address with regular expression
+14.5k Golang : Check if IP address is version 4 or 6
+3.7k Gogland : Datasource explorer