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
+24.7k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+26.3k Golang : Calculate future date with time.Add() function
+9k Golang : automatically figure out array length(size) with three dots
+12.4k Golang : 2 dimensional array example
+14.9k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+41.5k Golang : Convert string to array/slice
+14k Golang : concatenate(combine) strings
+25.9k Golang : How to read integer value from standard input ?
+17.9k Golang : Iterate linked list example
+8.6k Golang : Convert(cast) []byte to io.Reader type
+13.3k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message