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
+14.6k Golang : How to reverse elements order in map ?
+8.2k Golang : Read file with ioutil
+15.5k Golang : Gzip file example
+4.9k Get website traffic ranking with Similar Web or Alexa
+7.4k PHP : How to parse ElasticSearch JSON ?
+19.7k Curl usage examples with Golang
+22.4k Golang : Check if element exist in map
+15.1k Golang : Check if a string contains multiple sub-strings in []string?
+8.5k Golang : interface - when and where to use examples
+18.6k Android Studio : AlertDialog and EditText to get user string input example
+4.7k Golang : Markov chains to predict probability of next state example
+13.3k Golang : Parsing or breaking down URL