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)
}
写入自己的博客中才能记得长久