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 convert Go struct to JSON?
Regular Expression to get a string between parentheses in Golang
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
How to check if a string contains a white space in Golang?
How to check if a map contains a key in Go?
Different ways to convert Byte Array into String
Most Helpful This Week
Golang Get current Date and Time in EST, UTC and MST?How to blur an image in Golang?How to read current directory using Readdir?How to convert Go struct to JSON?Golang Slice interface and array concatenationHigher Order Functions in GolangHow to check if a string contains a white space in Golang?Split a string at uppercase letters using regular expression in GolangRegular expression to validate phone numberExample of Pointers with Struct