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
+18k How to enable MariaDB/MySQL logs ?
+7.7k Golang : Rot13 and Rot5 algorithms example
+13.8k Golang : reCAPTCHA example
+13.5k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+6.2k Golang : Missing Subversion command
+16.4k Golang : ROT47 (Caesar cipher by 47 characters) example
+6.7k Unix/Linux : How to get own IP address ?
+9k Golang : Executing and evaluating nested loop in html template
+5.6k Golang : fmt.Println prints out empty data from struct
+12.3k Golang : Pagination with go-paginator configuration example
+9.6k Golang : Web(Javascript) to server-side websocket example
+21.3k Golang : For loop continue,break and range