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
+26.9k Golang : Connect to database (MySQL/MariaDB) server
+5.6k Nginx : Password protect a directory/folder
+7.8k Golang : Get final balance from bit coin address example
+6k Golang : Levenshtein distance example
+33.9k Golang : Save image to PNG, JPEG or GIF format.
+23.5k Golang : Change file read or write permission example
+10.6k Golang : Verify Linux user password again before executing a program example
+6.2k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+50.6k Golang : How to get time in milliseconds?
+4.7k Python : Print unicode escape characters and string
+20.9k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7k Golang : Randomize letters from a string example