4.7.1 前缀和后缀
HasPrefix 判断字符串 s 是否以 prefix 开头:
strings.HasPrefix(s, prefix string) bool
HasSuffix 判断字符串 s 是否以 suffix 结尾:
strings.HasSuffix(s, suffix string) bool
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "This is an example of a string"
fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th")
fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
}
输出:
T/F? Does the string "This is an example of a string" have prefix Th? true
这个例子同样演示了转义字符 \ 和格式化字符串的使用。
字符串包含关系
Contains 判断字符串 s 是否包含 substr :
strings.Contains(s, substr string) bool
网友评论