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
+3.8k Golang : Experimental Jawi programming language
+8.3k Golang : Qt Yes No and Quit message box example
+8.9k Golang : Print how to use flag for your application example
+5.2k Golang : Detect face in uploaded photo like GPlus
+33.7k Golang : Save image to PNG, JPEG or GIF format.
+3k Golang : PGX CopyFrom to insert rows into Postgres database
+9.7k Golang : Generate random elements without repetition or duplicate
+32.3k Golang : All update packages with go get command
+18.5k Golang : Append content to a file
+5.1k Golang : Convert Chinese UTF8 characters to Pin Yin
+12k Golang : Count number of runes in string
+8.7k PHP : Get coordinates latitude/longitude from string