How to remove special characters from a string in GoLang?
Special characters typically include any character that is not a letter or number, such as punctuation and whitespace. Removing special characters from a string results in a string containing only letters and numbers.
Remove Special Characters
We use the ReplaceAllString() method from regex package to replace the matched non-alphanumeric characters with the empty string "".
Example
// Golang program to remove
// special characters from string
package main
import (
"fmt"
"regexp"
)
func main() {
str := "Golang@%Programs#"
str = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(str, "")
fmt.Println(str)
}
Output
GolangPrograms
Most Helpful This Week
Multiple-value <function> in single-value context error in Golang
Is There Still a Need for Programming in This AI World?
Find out how many logical processors used by current process
How do you handle HTTP client server compression in Go?
Cannot call non-function <variable> error in Golang
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
Most Helpful This Week
Passing multiple string arguments to a variadic functionRegular expression to extract filename from given path in GolangRegular expression to extract numbers from a string in GolangReplace first occurrence of string using RegexpPass different types of arguments in variadic functionHow to check lowercase characters in a string in Golang?How to get struct variable information using reflect package?Regular expression to extract DNS host-name or IP Address from stringHow to convert Colorful PNG image to Gray-scale?How to use function from another file golang?