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
+7.8k Javascript : How to check a browser's Do Not Track status?
+6.8k Golang : Normalize email to prevent multiple signups example
+20.6k Golang : Read directory content with os.Open
+12.3k Golang : Extract part of string with regular expression
+10k Golang : Compare files modify date example
+11.3k Use systeminfo to find out installed Windows Hotfix(s) or updates
+11.7k Golang : convert(cast) float to string
+8.2k Golang : Number guessing game with user input verification example
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+9.5k Golang : Read file with ioutil
+10.9k Golang : Create Temporary File
+10.1k Golang : Check a web page existence with HEAD request example