英文:
Splitting a string on a list of strings in Go
问题
可以将字符串切割成多个子字符串,不仅限于一个字符串,还可以使用一个字符串切片。例如:
strings.Split("Dogs and Cats are Great", []string{"and", "are"})
这样就可以将字符串 "Dogs and Cats are Great" 按照 "and" 和 "are" 进行切割。
英文:
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"}))
答案1
得分: 5
你可以使用正则表达式:http://play.golang.org/p/vCRCv4rt7s
re := regexp.MustCompile(`and|are`)
fmt.Printf("%q\n", re.Split("Dogs and Cats are Great", -1))
英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论