HasPrefix 判断字符串 s 是否以 prefix 开头:strings.HasPrefix(s, prefix string) bool
HasSuffix 判断字符串 s 是否以 suffix 结尾:strings.HasSuffix(s, suffix string) bool
字符串包含关系
Contains 判断字符串 s 是否包含 substr :strings.Contains(s, substr string) bool
测试源码:
package main import ( "fmt" "strings" ) func main() { var str1 string = "This is an example of a string" var str2 string = "this is an " var str3 string = " of a string" fmt.Println(strings.HasPrefix(str1, str2)) fmt.Println(strings.HasSuffix(str1, str3)) }
运行结果:
[root@localhost golang]# go run zzz.go false true