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, #@ ! ^&Programming (*)."
    
    s = removeNonAlphanumeric(s)
    
    fmt.Println(s) 
}



/*
run:

GoProgramming

*/

 



answered Jan 29, 2025 by avibootz
edited Jan 29, 2025 by avibootz
...