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
+6.2k PHP : Proper way to get UTF-8 character or string length
+17k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+31.1k Golang : Get local IP and MAC address
+7.7k Golang : Check from web if Go application is running or not
+7.5k Golang : Test if an input is an Armstrong number example
+14k Android Studio : Use image as AlertDialog title with custom layout example
+22.2k Golang : Convert Unix timestamp to UTC timestamp
+17.9k Golang : Get command line arguments
+4.1k Javascript : How to show different content with noscript?
+6.3k Golang : Embedded or data bundling example
+6.2k Golang : Break string into a slice of characters example
+36.1k Golang : Convert date or time stamp from string to time.Time type