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
+6.3k CodeIgniter : form input set_value cause " to become & quot
+18.8k Golang : Read input from console line
+26.2k Golang : Convert(cast) string to uint8 type and back to string
+13.2k Golang : Verify token from Google Authenticator App
+18.7k Golang : Display list of time zones with GMT
+10.2k Golang : cannot assign type int to value (type uint8) in range error
+15.5k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+7.7k Golang : Getting Echo framework StartAutoTLS to work
+10.5k Golang : How to delete element(data) from map ?
+6k Java : Human readable password generator
+15.8k Golang : Read a file line by line
+5.7k Linux : Disable and enable IPv4 forwarding