3
是否可以拆分一個字符串而不是一個字符串片段?即在Go中的字符串列表中拆分字符串
strings.Split("Dogs and Cats are Great", "and"))
,而不是使用一個字符串,字符串的切片爲使但是:
strings.Split("Dogs and Cats are Great", []string{"and", "are"}))
是否可以拆分一個字符串而不是一個字符串片段?即在Go中的字符串列表中拆分字符串
strings.Split("Dogs and Cats are Great", "and"))
,而不是使用一個字符串,字符串的切片爲使但是:
strings.Split("Dogs and Cats are Great", []string{"and", "are"}))
您可以使用正則表達式:http://play.golang.org/p/vCRCv4rt7s
re := regexp.MustCompile(`and|are`)
fmt.Printf("%q\n", re.Split("Dogs and Cats are Great", -1))
這似乎是一個完美的解決方案。我想知道,是否有一種方法可以在單詞之前進行分割,而不是在拆分中將其替換掉? –
@詹姆斯米爾納:請顯示你的意思是「拆分之前」。 – JimB
I.e.返回「狗」,「和貓」,「很棒」,而不是「狗」,「貓」,「很棒」 –