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 Make an HTTP Server in Golang?
GO Program to Find LCM and GCD of given two numbers
Cannot convert <type1> to <type2> error in Golang
How do you handle HTTP requests in Go?
How to remove all line breaks from a string in Golang?
How do you set cookies in an HTTP request with an HTTP client in Go?
Most Helpful This Week
How to check if a map contains a key in Go?What is Rune? How to get ASCII value of any character in Go?Golang Slice interface and array concatenationHow to fetch an Integer variable as String in Go?How to get the current date and time with timestamp in local and other timezones ?How to handle HTTP Get response?How do you write multi-line strings in Go?How To Make an HTTP Server in Golang?How to create Map using the make function in Go?Regular Expression to get a string between parentheses in Golang