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
How to create Slice of Struct in Golang?
Multiple-value <function> in single-value context error in Golang
Read and Write Fibonacci series to Channel in Golang
GO Program to Calculate Area of Rectangle and Square
Golang Program to print Triangle of Numbers
How do you handle HTTP authentication with an HTTP client in Go?
What is state in React?
How do you create an HTTP server in Go?
Undefined <variable/function> error in Golang
Sierpinski Carpet in Go Programming Language
Most Helpful This Week
How to use array in Go Programming Language?Regular expression to validate the date format in "dd/mm/yyyy"How to kill execution of goroutine?Convert Int data type to Int16 Int32 Int64Convert Float32 to Float64 and Float64 to Float32Sierpinski triangle in Go Programming LanguageExample: Fields and FieldsFunc from BYTES PackageExample: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of SlicesPassing multiple string arguments to a variadic functionRegular expression to extract domain from URL