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
+20.8k Golang : Saving private and public key to files
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+5.3k Golang : Experimental Jawi programming language
+9.1k Golang : Build and compile multiple source files
+6.7k Golang : How to determine if request or crawl is from Google robots
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+19.5k Golang : How to count the number of repeated characters in a string?
+48.3k Golang : How to convert JSON string to map and slice
+9.3k Golang : Write multiple lines or divide string into multiple lines
+7.7k Golang : Convert(cast) io.Reader type to string
+6.6k Elasticsearch : Shutdown a local node
+10.7k Golang : Select region of interest with mouse click and crop from image