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
How to get the current date and time with timestamp in local and other timezones ?
How to concatenate two or more slices in Golang?
How to get Dimensions of an image type jpg jpeg png or gif ?
Various examples of printing and formatting in Golang
Regular expression to validate common Credit Card Numbers
The return values of a function can be named in Golang
How to check if a string contains a substring in Golang?
How to blur an image in Golang?
How to read/write from/to file in Golang?
Simple function with parameters in Golang
Most Helpful This Week
How to set timeout for http.Get() requests in Golang?Regular expression to extract numbers from a string in GolangHow to print struct variables data in Golang?Data encryption with AES-GCMCatch values from GoroutinesHow to find length of Map in Go?How to import and alias package names?Regular expression to validate the date format in "dd/mm/yyyy"How to convert Go struct to JSON?How can we reverse a simple string in Go?