How to remove all non-alphanumeric characters from a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "regexp"
)
 
func removeNonAlphanumeric(str string) string {
    re := regexp.MustCompile("[^a-zA-Z0-9]+")
     
    return re.ReplaceAllString(str, "")
}
 
func main() {
    s := "Go, 672#@ ! ^&Programming (*)."
     
    s = removeNonAlphanumeric(s)
     
    fmt.Println(s) 
}
 
 
 
/*
run:
 
Go672Programming
 
*/

 



answered Jan 29, 2025 by avibootz
edited 1 day ago by avibootz

Related questions

...