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
+1.9k Golang : How to check if a string with spaces in between is numeric?
+9k Golang : Check if a string contains multiple sub-strings in []string?
+3.5k PHP : Shuffle to display different content or advertisement
+5.9k Golang : Convert(cast) uintptr to string example
+29.1k Golang : Encode image to base64 example
+16.2k Golang : Get and Set User-Agent examples
+8.6k Golang : How to extract links from web page ?
+3.6k Golang : Eroding and dilating image with OpenCV example
+3.7k Golang : Routes multiplexer routing example with regular expression control
+2.5k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+4.3k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+4.4k Golang : Simple image viewer with Go-GTK