go - 从头到尾创建子字符串

标签 go

我有一个字符串。

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/

相关文章:

emacs - 使用 go-mode 自动完成

go - 这是一个合理且惯用的 GoLang 循环移位实现吗?

html - 如何使用encoding/xml忽略Go中的html属性?

html - 图片没有显示在 html 上用 golang 上的图片书写

open-source - Go 代码贡献 : license and patent implications?

go - 如何使用 golang id3-go 库获取和打印 mp3 Popularimeter 框架

linux - 异步获取 "slow protocols"的目录条目

go - gvm 在 ubuntu 14.04 中设置 GOPATH

go - 具有相同接收器函数实现的两个结构,如何删除重复项

go - 使用 go-oauth2/oauth2 库生成 JWT 刷新 token