golang strings.HasPrefix()方法

strings.HasPrefix()方法

Golang的标准库strings中的HasPrefix()方法,可以用于判断字符串是否以某个子串为前缀。


HasPrefix()语法

func HasPrefix(s, prefix string) bool

go源码对HasPrefix()方法的介绍:

HasPrefix tests whether the string s begins with prefix.

参数

参数描述
sstring字符串类型
prefixstring字符串类型,判断s字符串是否以prefix开始,为前缀

返回值

bool类型,如果字符串s以prefix为前缀,则返回true,否则返回false。


strings.HasPrefix()方法实例代码

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-2024 笨鸟工具 x1y1z1.com All Rights Reserved.