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
+5.7k Golang : Find change in a combination of coins example
+11.9k Golang : Find and draw contours with OpenCV example
+9.4k Facebook : Getting the friends list with PHP return JSON format
+16.7k Golang : read gzipped http response
+5.3k Swift : Convert string array to array example
+11.4k Golang : Change date format to yyyy-mm-dd
+28.5k Get file path of temporary file in Go
+12.2k Golang : List running EC2 instances and descriptions
+10.4k Generate Random number with math/rand in Go
+9.6k Random number generation with crypto/rand in Go
+18.5k Golang : Iterating Elements Over A List
+20.5k Android Studio : AlertDialog and EditText to get user string input example