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
Regular expression to extract all Non-Alphanumeric Characters from a String
How to read names of all files and folders in current directory?
Example: How to use TeeReader from IO Package in Golang?
How to get current IP form ipify.org ?
How to create a photo gallery in Go?
How can I convert a string variable into Boolean, Integer or Float type in Golang?
Most Helpful This Week
Convert Float32 to Float64 and Float64 to Float32Regular expression to extract text between square bracketsGet Set and Clear Session in GolangExample: How to use ReadAtLeast from IO Package in Golang?How to get Dimensions of an image type jpg jpeg png or gif ?How to use a mutex to define critical sections of code and fix race conditions?Split URL and Get Parameters from URLRegular expression to extract numbers from a string in GolangWhat is GOPATH?Golang Get current Date and Time in EST, UTC and MST?