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 use function from another file golang?
Catch values from Goroutines
How can I convert a string variable into Boolean, Integer or Float type in Golang?
How to check string contains uppercase lowercase character in Golang?
Find element in a slice and move it to first position?
How to print struct variables data in Golang?
Most Helpful This Week
How can we reverse a simple string in Go?How to check UPPERCASE characters in a string in Golang?How to get first and last element of slice in Golang? How to kill execution of goroutine?Example: How to use ReadFull from IO Package in Golang?How to read/write from/to file in Golang?How to extract text from between html tag using Regular Expressions in Golang?How to check if a map contains a key in Go?How to delete or remove element from a Map?Regular expression to extract domain from URL