我试图在字符串匹配后获取下一个单词。例如var somestring string = "Hello this is just a random string"匹配我这样做的词strings.Contains(somestring, "just")在这种情况下,我想得到“a”。
1 回答
慕少森
TA贡献2019条经验 获得超9个赞
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello this is just a random string"
// find the index of string "just"
index := strings.Index(s, "just")
fmt.Println(index)
// get next single word after "just"
word := strings.Fields(s[index:])
fmt.Println(word[1])
}
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报
0/150
提交
取消