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
+31.3k Golang : Regular Expression for alphanumeric and underscore
+5.9k Golang : Calculate BMI and risk category
+4.4k Python : Delay with time.sleep() function example
+34.2k Golang : Validate IP address
+19.2k Golang : Pipe output from one os.Exec(shell command) to another command
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+7.3k Golang : Ackermann function example
+8.2k Golang : How to control fmt or log print format?
+14k Golang : Get timezone offset from date or timestamp
+17k Golang : Iterating Elements Over A List
+13.2k Golang : GUI with Qt and OpenCV to capture image from camera