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
423 Golang : Fix go-cron set time not working issue
+8.8k Golang : Pagination with go-paginator configuration example
+3.2k Golang : Get FX sentiment from website example
+4k Javascript : How to refresh page with JQuery ?
+4.7k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+9.6k Golang : Search and extract certain XML data example
+32k Upload multiple files with Go
+23.7k Golang : Encrypt and decrypt data with AES crypto
+12k Golang : Parsing or breaking down URL
+13.4k Golang : How to extract links from web page ?
+2.9k Javascript : How to show different content with noscript?
+3.9k Fix yum-complete-transaction error