How to remove symbols from a string in Golang?
Remove symbols from a given string
In the following program ReplaceAllString() method is used, which allows us to replace original string with another string if the specified string matches with the specified regular expression. This method is defined under the regexp package, hence to access ReplaceAllString() method first we need to import the regexp package in our program.
Example
package main
import (
"fmt"
"log"
"regexp"
)
func main() {
str1 := "how much for the maple syrup? $20.99? That's ridiculous!!!"
re, err := regexp.Compile(`[^\w]`)
if err != nil {
log.Fatal(err)
}
str1 = re.ReplaceAllString(str1, " ")
fmt.Println(str1)
}
Output
how much for the maple syrup 20 99 That s ridiculous
Most Helpful This Week
Convert Float32 to Float64 and Float64 to Float32
How to use a mutex to define critical sections of code and fix race conditions?
Golang HTTP GET request with parameters
Golang Get current Date and Time in EST, UTC and MST?
Example: How to use TeeReader from IO Package in Golang?
Example to compare Println vs Printf
Most Helpful This Week
How to Remove duplicate values from Slice?How to read input from console line?How to check lowercase characters in a string in Golang?Closures Functions in GolangReplace numbers by zero from stringHow to fetch an Integer variable as String in Go?How to find out element position in slice?How to append text to a file in Golang?Regular expression for matching HH:MM time format in GolangData encryption with AES-GCM