Golang 截取 URL 字符串 - 获取 html 后第一个 & 前的内容
您可以使用 strings 包中的函数来实现该功能。具体的方法如下:
package main
import (
"fmt"
"strings"
)
func main() {
url := "https://game.weixin.qq.com/cgi-bin/h5/static/appcenter/act_special.html?clusterid=10000036\u0026wechat_pkgid=appcenter_act_special\u0026ssid=3150\u0026immersivePageBgIsDark=0\u0026wechat_immersive=1\u0026immersiveUIStyle=1\u0026pre_inject_data=%5Bobject+Object%5D\u0026preInjectData=%5Bobject+Object%5D\u0026abt=64\u0026rpt_allpath=16304_999999_3150_999999_3150_8812_2881_8812_3154_2890_3150\u0026from_wechat_pkgid=pureprofile_index"
// 截取“html”之后的第一个“&”或者\u0026前面的内容
index := strings.Index(url, "html")
if index != -1 {
substr := url[index+4:]
index = strings.Index(substr, "&")
if index != -1 {
result := substr[:index]
fmt.Println(result)
} else {
fmt.Println(substr)
}
}
}
输出结果为:
?clusterid=10000036\u0026wechat_pkgid=appcenter_act_special
这样就成功截取了“html”之后的第一个“&”或者\u0026前面的内容。
原文地址: https://www.cveoy.top/t/topic/pKxU 著作权归作者所有。请勿转载和采集!