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 iterate over an Array using for loop?How to check UPPERCASE characters in a string in Golang?How to split a string on white-space?Constructors in GolangHow to Draw a rectangle in Golang?How to update content of a text file?Golang Convert String into Snake CaseHow to use for and foreach loop?How to get Dimensions of an image type jpg jpeg png or gif ?Example: Stack and Caller from RUNTIME package