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
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+5.7k Javascript : How to replace HTML inside <div>?
+21.6k Golang : GORM create record or insert new record into database example
+9k Golang : Get curl -I or head data from URL example
+26.2k Golang : Calculate future date with time.Add() function
+20.6k Golang : Saving private and public key to files
+7k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+4.3k Golang : Valued expressions and functions example
+8.9k Golang : Go as a script or running go with shebang/hashbang style
+23.4k Golang : Read a file into an array or slice example
+9k Golang : Gonum standard normal random numbers example
+11k Golang : Roll the dice example