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
+13k Golang : Date and Time formatting
+12k Golang : calculate elapsed run time
+15.7k Golang : How to reverse elements order in map ?
+5.1k Python : Convert(cast) string to bytes example
+12.4k Golang : Sort and reverse sort a slice of bytes
+7.6k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+6.6k Fix sudo yum hang problem with no output or error messages
+41.2k Golang : Convert string to array/slice
+6.1k CodeIgniter : form input set_value cause " to become & quot
+5.5k Golang : Struct field tags and what is their purpose?
+39.2k Golang : Remove dashes(or any character) from string
+32.6k Golang : How to check if a date is within certain range?