我有一个字符串。
s = "This is a super dog. This dog is very nice. He play with other dogs. He love to eat."
golang 中的 substring() 函数接受字符串和子字符串。但是,如果我想为我的子字符串指定开始和结束字符串怎么办?
例如,在这个字符串s中,我希望子串从“super”开始,以“love”结束。所以我的子字符串应该是,
substring = "super dog. This dog is very nice. He play with other dogs. He love"
我没有看到 golang 提供任何此类功能。如果有办法做到这一点,请告诉我。
最佳答案
你可以尝试这样的事情:
start := strings.Index(s, "super")
end := strings.Index(s[start:], "love")
fmt.Println(s[start:end] + "love") // s[start:end+len("love")] performs better
参见 here有关详细信息和更多类似方法。
关于go - 从头到尾创建子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450432/