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 for and foreach loop?
Create and Print Multi Dimensional Slice in Golang
Golang Read Write and Process data in CSV
Sierpinski triangle in Go Programming Language
How to use function from another file golang?
How to append text to a file in Golang?
How to Convert string to float type in Go?
Creating a Function in Golang
Get Year, Month, Day, Hour, Min and Second from current date and time.
Dereferencing a pointer from another package
Most Helpful This Week
How to get current IP form ipify.org ?Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]How to wait for Goroutines to Finish Execution?Find capacity of Channel, Pointer and SliceRegular Expression to get a string between parentheses in GolangRegular expression to validate the date format in "dd/mm/yyyy"Golang HTTP GET request with parametersHow to check if a string contains a numbers in Golang?How to use function from another file golang?How to print struct variables data in Golang?