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
+29k Golang : missing Git command
+7.4k Golang : Dealing with struct's private part
+5.6k Unix/Linux : Get reboot history or check when was the last reboot date
+39.4k Golang : Remove dashes(or any character) from string
+8.1k Golang : Metaprogramming example of wrapping a function
+8.1k Golang : Oanda bot with Telegram and RSI example
+18.1k Golang : How to remove certain lines from a file
+9.3k Golang : Changing a RGBA image number of channels with OpenCV
+16.2k Golang :Trim white spaces from a string
+7.8k Golang Hello World Example
+28.3k Golang : Read, Write(Create) and Delete Cookie example
+21.5k SSL : How to check if current certificate is sha1 or sha2