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
+10.7k Golang : Compress and decompress file with compress/flate example
+41.5k Golang : Encode image to base64 example
+4.8k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+8.1k Golang : Get UDP client IP address and differentiate clients by port number
+19.4k Golang : Get ASCII code from a key press(cross-platform) example
+12.6k Golang : How to convert(cast) IP address to string?
+15.5k How to enable MariaDB/MySQL logs ?
+21.1k Find and replace a character in a string in Go
+27.9k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+13.5k Golang : Check if a string contains multiple sub-strings in []string?
+5.8k Golang : Individual and total number of words counter example
+5.7k Golang : Sort words with first uppercase letter