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
+20.8k Golang : Secure(TLS) connection between server and client
+13.9k Golang : Check if an integer is negative or positive
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+20.5k Golang : How to get own program name during runtime ?
+11.4k Golang : How to pipe input data to executing child process?
+14.1k Golang : Human readable time elapsed format such as 5 days ago
+12.9k Golang : zlib compress file example
+14.9k Golang : Send email with attachment(RFC2822) using Gmail API example
+9.4k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+5k Python : Find out the variable type and determine the type with simple test
+14.5k Golang : On enumeration