James Milner
James Milner

Reputation: 899

Splitting a string on a list of strings in Go

Is it possible to split not just on one string but also a slice of strings? I.e.

strings.Split("Dogs and Cats are Great", "and"))

But instead of using one string, a slice of strings as so:

strings.Split("Dogs and Cats are Great", []string{"and", "are"}))

Upvotes: 3

Views: 163

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109347

You can use a regular expression: http://play.golang.org/p/vCRCv4rt7s

re := regexp.MustCompile(`and|are`)
fmt.Printf("%q\n", re.Split("Dogs and Cats are Great", -1))

Upvotes: 5

Related Questions