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
+7.1k Golang : Calculate how many weeks left to go in a given year
+22.3k Generate checksum for a file in Go
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+14.1k Golang : How to convert a number to words
+4.5k Facebook : How to place save to Facebook button on your website
+45.8k Golang : Read tab delimited file with encoding/csv package
+5.9k Golang : Create new color from command line parameters
+13.7k Golang : How to check if a file is hidden?
+9.6k Golang : ffmpeg with os/exec.Command() returns non-zero status
+8.7k Golang : Find network service name from given port and protocol
+26.4k Golang : Encrypt and decrypt data with AES crypto
+6.3k Grep : How to grep for strings inside binary data