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
+15.3k Golang : How to make function callback or pass value from function as parameter?
+9.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+5.6k Golang : Scramble and unscramble text message by randomly replacing words
+13k Golang : Get own process identifier
+7k Golang : Intercept and process UNIX signals example
+3.7k Get website traffic ranking with Similar Web or Alexa
+3.7k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+5.5k Golang : Gaussian blur on image and camera video feed examples
+7.9k Golang : Verify Linux user password again before executing a program example
+9.2k SSL : The certificate is not trusted because no issuer chain was provided
+7.8k Golang : Convert IPv4 address to packed 32-bit binary format
+3.7k Golang : Example of how to detect which type of script a word belongs to