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
+4.2k Golang : Calculate half life decay example
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+11.8k Android Studio : Password input and reveal password example
+8.8k Golang : Check if user agent is a robot or crawler example
+12.4k Golang : Set image canvas or background to transparent
+9.5k Swift : Convert (cast) String to Integer
+7.7k Golang : Accept any number of function arguments with three dots(...)
+9.1k Golang : Read file and convert content to string
+12.9k Golang : Get uploaded file name or access uploaded files
+19.7k Golang : How to read float value from standard input ?