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
+25.2k Golang : Find files by extension
+27.8k Golang : How to create new XML file ?
+11.1k Golang : convert(cast) string to integer value
+14k Golang : Get digits from integer before and after given position example
+17.2k Golang : Write file with io.WriteString
+5.8k Fix sudo yum hang problem with no output or error messages
+12.9k Golang : Google Drive API upload and rename example
+27.5k Golang : Saving(serializing) and reading file with GOB
+4.3k Golang : Issue HTTP commands to server and port example
+20.7k Golang : How to run Golang application such as web server in the background or as daemon?
+14.3k Golang : invalid character ',' looking for beginning of value
+3.7k Linux : sudo yum updates not working