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
+10k Golang : Embed secret text string into binary(executable) file
+4.8k Golang : Calculate a pip value and distance to target profit example
+7.7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+15.3k Golang : Force download file example
+16.3k Golang : File path independent of Operating System
+23.8k Golang : Find biggest/largest number in array
+4k Javascript : Empty an array example
+6.3k Unix/Linux : How to get own IP address ?
+6.7k Golang : Decode XML data from RSS feed
+5k Golang : Print instead of building pyramids
+7.3k Golang : Rename part of filename
+9k Golang : How to check if a string with spaces in between is numeric?