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
+21.3k Golang : Use TLS version 1.2 and enforce server security configuration over client
+7.3k Golang : Error reading timestamp with GORM or SQL driver
+5.7k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+13.8k Golang : Get uploaded file name or access uploaded files
+15.6k Golang : How to check if input from os.Args is integer?
+9.6k Golang : Get escape characters \u form from unicode characters
+19.5k Swift : Convert (cast) Int to int32 or Uint32
+5.2k Python : Print unicode escape characters and string
+5.5k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+4.8k PHP : See installed compiled-in-modules
+18k Golang : Display list of time zones with GMT
+14.1k Golang : Find network of an IP address