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
+16.7k Golang : Set up source IP address before making HTTP request
+7.6k Golang : How to execute code at certain day, hour and minute?
+12.3k Golang : flag provided but not defined error
+8.9k Golang : Get SPF and DMARC from email headers to fight spam
+12k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+16.3k Golang : Send email and SMTP configuration example
+7.1k Golang : How to fix html/template : "somefile" is undefined error?
+20.6k Golang : Saving private and public key to files
+4.9k Golang : PGX CopyFrom to insert rows into Postgres database
+9.9k Golang : Edge detection with Sobel method
+5.3k Golang : Intercept, inject and replay HTTP traffics from web server
+15.5k Golang : Get checkbox or extract multipart form data value example