Golang的标准库strings中的HasPrefix()方法,可以用于判断字符串是否以某个子串为前缀。
func HasPrefix(s, prefix string) bool
go源码对HasPrefix()方法的介绍:
HasPrefix tests whether the string s begins with prefix.
参数 | 描述 |
---|---|
s | string字符串类型 |
prefix | string字符串类型,判断s字符串是否以prefix开始,为前缀 |
bool类型,如果字符串s以prefix为前缀,则返回true,否则返回false。
package main
import (
"fmt"
"strings"
)
func main() {
var t1 bool = strings.HasPrefix("笨鸟工具,x1y1z1.com", "笨鸟")
fmt.Println(t1)
t2 := strings.HasPrefix("abc", "ef")
fmt.Println(t2)
}
//命令行输入运行go文件的命令,比如:go run test.go,得到输出:
true
false
全栈编程 / go语法(可向下滚动):
Copyright © 2022-2023 笨鸟工具 x1y1z1.com All Rights Reserved.