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
+6k Golang : How to iterate a slice without using for loop?
+7.8k Golang : Write multiple lines or divide string into multiple lines
+10.8k Golang : Print UTF-8 fonts on image example
+19.9k Golang : GORM create record or insert new record into database example
+17.5k Unmarshal/Load CSV record into struct in Go
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+8.3k Golang : Qt Yes No and Quit message box example
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+10.4k Golang : Generate DSA private, public key and PEM files example
+7.6k Golang : Progress bar with ∎ character
+18.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)