Golang : Remove dashes(or any character) from string
Problem :
You have a string with dashes in between and you want to remove all the dashes. How to do that?
Solution :
Use strings.Replace()
function to remove the dashes(or any character). See http://golang.org/pkg/strings/#Replace on how to configure the parameter.
package main
import (
"fmt"
"strings"
)
func main() {
strWithDashes := "0-201-53377-4"
// remove all dashes
// -1 means, all occurrences
noDashes := strings.Replace(strWithDashes, "-", "", -1)
fmt.Println("Before : ", strWithDashes)
fmt.Println("After : ", noDashes)
}
Output :
Before : 0-201-53377-4
After : 0201533774
Reference :
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
+13.8k Golang : Set image canvas or background to transparent
+21.5k Golang : How to force compile or remove object files first before rebuild?
+7k Default cipher that OpenSSL used to encrypt a PEM file
+32.6k Golang : Math pow(the power of x^y) example
+18.8k Unmarshal/Load CSV record into struct in Go
+12.5k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+26.8k Golang : How to check if a connection to database is still alive ?
+25.3k Golang : Storing cookies in http.CookieJar example
+9.1k Golang : Get SPF and DMARC from email headers to fight spam
+17k Golang : How to generate QR codes?
+5.3k Golang : The Tao of importing package
+6k Unix/Linux : How to open tar.gz file ?