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
+16.2k Golang : How to remove certain lines from a file
+18.4k Swift : Convert (cast) Int to int32 or Uint32
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+5k Golang : Detect variable or constant type
+10.6k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+10.3k Golang : Display a text file line by line with line number example
+4.5k Swift : Convert string array to array example
+12.7k Golang : How to convert a number to words
+20.8k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+7.6k Golang : Combine slices but preserve order example
+5k Golang : Generate multiplication table from an integer example