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
Different ways for Integer to String Conversions
What is Struct
How to declare Interface Type in Go Programming Language
Golang Program to print Triangle of Numbers
Go program to reading plain text files
Sierpinski triangle in Go Programming Language
How to set timeout for http.Get() requests in Golang?
How do you handle HTTP Client server load balancing in Go?
How to create Slice using new keyword in Golang?
GO Program to Calculate Sum of Natural Numbers Using for.....Loop
Most Helpful This Week
Example of Switch Case with Break in For LoopHow to convert Boolean Type to String in Go?How to import and alias package names?Golang download image from given URLGet Year, Month, Day, Hour, Min and Second from a specified dateSierpinski triangle in Go Programming LanguageHow to copy a map to another map?Split a character string based on change of characterHow to use for and foreach loop?Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]