举例来说,我有一个字符串,如下所示:
<h1>Hello World!</h1>
Go 代码能够提取什么Hello World!
从那个字符串?我对 Go 还比较陌生。任何帮助是极大的赞赏!
如果字符串看起来像whatever;START;extract;END;whatever,你可以使用它来获取中间的字符串:
// GetStringInBetween Returns empty string if no start string found
func GetStringInBetween(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
return
}
s += len(start)
e := strings.Index(str[s:], end)
if e == -1 {
return
}
e += s + e - 1
return str[s:e]
}
这里发生的事情是它将找到 START 的第一个索引,添加 START 字符串的长度并返回从那里到 END 的第一个索引为止存在的所有内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)