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
Get current date and time in various format in golang
Example to compare Println vs Printf
Regular expression to validate common Credit Card Numbers
Example: ReadAll, ReadDir, and ReadFile from IO Package
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
Regular expression for matching HH:MM time format in Golang
How to convert Go struct to JSON?
Example: How to use TeeReader from IO Package in Golang?
How to handle HTTP Get response?
How to find length of Map in Go?
Most Helpful This Week
Strip all white spaces, tabs, newlines from a stringHow to fetch an Integer variable as String in Go?How to print struct variables data in Golang?Example to compare Println vs PrintfSelect single argument from all arguments of variadic functionSubtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.Passing multiple string arguments to a variadic functionExample to use Weekday and YearDay functionHow to check if a string contains certain characters in Golang?How to use array in Go Programming Language?