url截取域名

XiaoBaiBai · 2022-02-25 10:46:03 · 1139 次点击

按理说 com.cn.cn 的二级域名。

#4
更多评论
str := "www.baidu.com.cn"
index := strings.Index(str, ".com")
sub := str[index:]
fmt.Println(sub)
#1
func getUrl(str string) string{
    l := strings.Split(str,".")
    if l[len(l)-1] =="com" || l[len(l)-1] =="cn"{
        if l[len(l)-2]=="com"{
            tmp:= l[len(l)-2:]
            return fmt.Sprintf(".%s.%s",tmp[0],tmp[1])
        }else{
            tmp:= l[len(l)-1:]
            return fmt.Sprintf(".%s",tmp[0])
        }

    }
}
#2