go字符串判断开头strings.HasPrefix(url, "https://")和结尾strings.HasSuffix(url, "/")

1,判断开头strings.HasPrefix(url, "https://")

点击查看代码
package main

import (
	"fmt"
	"strings"
)

func urlProcess(url string) string {
	if !strings.HasPrefix(url, "https://") {
		url = fmt.Sprintf("https://%s", url)
	}
	return url
}

func main() {
	var str string
	fmt.Scanf("%s", &str)
	str = urlProcess(str)
	fmt.Println(str)
}

2, 判断结尾strings.HasSuffix(url, "/")

点击查看代码
package main

import (
	"fmt"
	"strings"
)

func pathProcess(url string) string {
	if !strings.HasSuffix(url, "/") {
		url = fmt.Sprintf("%s/", url)
	}
	return url
}

func main() {
	var str string
	fmt.Scanf("%s", &str)
	str = pathProcess(str)
	fmt.Println(str)
}
posted @ 2022-02-27 13:05  ty1539  阅读(648)  评论(0编辑  收藏  举报