remove a substring from a string in go

main.go
import "regexp"

func removeSubstring(s string, sub string) string {
    re := regexp.MustCompile(sub)
    return re.ReplaceAllString(s, "")
}
143 chars
7 lines

You can use the regexp package in Go to remove a substring from a string. The regexp.MustCompile() function compiles a regular expression pattern, and ReplaceAllString() replaces all occurrences of the pattern with the replacement string (in this case, an empty string).

gistlibby LogSnag