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
+13.2k Golang : Google Drive API upload and rename example
+4.6k Unix/Linux/MacOSx : How to remove an environment variable ?
+9.5k Android Studio : Simple input textbox and intercept key example
+8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+7k Golang : Scan files for certain pattern and rename part of the files
+12.7k Golang : Set image canvas or background to transparent
+15.7k Golang : How to implement two-factor authentication?
+9k PHP : Get coordinates latitude/longitude from string
+9k Golang : How to check if a website is served via HTTPS
+6.3k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+11k Golang : Convert decimal number(integer) to IPv4 address
+7.1k Golang : Get all countries phone codes