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
+4.8k Golang : Not able to grep log.Println() output
+9.5k Javascript : Prompt confirmation before exit
+7.5k Golang : How to delete element(data) from map ?
+4.5k Fix sudo yum hang problem with no output or error messages
+13.9k Golang : Read a file into an array or slice example
+14k Golang : Check if os.Stdin input data is piped or from terminal
+3.2k Golang : Markov chains to predict probability of next state example
+2.7k Facebook : How to place save to Facebook button on your website
+9.7k Golang : Send email with attachment(RFC2822) using Gmail API example
+4.7k Golang : Number guessing game with user input verification example
+3.7k Golang : What is StructTag and how to get StructTag's value?
+3.8k Facebook : How to force facebook to scrape latest URL link data?