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
+11.6k Golang : Arithmetic operation with numerical slices or arrays example
+26.4k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+4.5k Golang : How to deal with configuration data?
+16.5k Golang : Linked list example
+3.9k JavaScript : Rounding number to decimal formats to display currency
+29.7k Golang : Interpolating or substituting variables in string examples
+29.2k Golang : Remove characters from string example
+4.5k Golang : Issue HTTP commands to server and port example
+16.7k How to enable MariaDB/MySQL logs ?
+5k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+19.6k Golang : Saving private and public key to files
+6.8k Gogland : Single File versus Go Application Run Configurations