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
+29.6k Golang : How to create new XML file ?
+9k Golang : Gaussian blur on image and camera video feed examples
+9.2k Golang : Gonum standard normal random numbers example
+8.2k Golang : Append and add item in slice
+7.9k Golang : Example of how to detect which type of script a word belongs to
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+13.9k Golang : Gin framework accept query string by post request example
+16.6k Golang : Get IP addresses of a domain name
+14.9k Golang : Normalize unicode strings for comparison purpose
+7.4k Golang : How to iterate a slice without using for loop?
+12.2k Golang : Pagination with go-paginator configuration example