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
+9.7k Golang : Channels and buffered channels examples
+8.9k Android Studio : Indicate progression with ProgressBar example
+5.1k Golang : Intercept, inject and replay HTTP traffics from web server
+4k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+17.1k Golang : Get future or past hours, minutes or seconds
+12.9k Golang : Convert(cast) int to int64
+14.1k Golang : Parsing or breaking down URL
+31.1k Golang : Get local IP and MAC address
+36.1k Golang : Convert(cast) int64 to string
+5.6k Facebook : How to force facebook to scrape latest URL link data?
+5.6k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+16.2k Golang : Read integer from file into array