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
+11.3k Golang : Validate email address
+12.6k Golang : Check if an integer is negative or positive
+28k Golang : JQuery AJAX post data to server and send data back to client example
+7.6k Golang : Progress bar with ∎ character
+7.7k Golang : Gaussian blur on image and camera video feed examples
+4.4k Golang : How to deal with configuration data?
+12.4k Golang : Strings comparison
+6.2k Nginx : How to block user agent ?
+33.6k Golang : Upload and download file to/from AWS S3
+9.6k Golang : Replace a parameter's value inside a configuration file example
+21.7k Golang : Strings to lowercase and uppercase example
+9.1k Golang : Get login name from environment and prompt for password