go strings.IndexByte()方法

strings.IndexByte()方法

Golang标准库模块strings的IndexByte()方法可查找字符c在字符串s中第一次出现的索引位置,如果字符串s不包含字符c,则返回-1。


语法

func IndexByte(s string, c byte) int

go源码Index()方法的介绍:

IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.

参数

参数描述
sstring字符串类型
cgo byte类型,与uint8等价

返回值

字符在字符串中的索引位置,或-1。


strings.IndexByte()方法实例代码

func main() {
	str := "笨鸟工具,x1y1z1.com"
	str2 := []byte(str)
	fmt.Println((str2))
	var I1 int = strings.IndexByte(str, 231)
	fmt.Println(I1)
	I2 := strings.IndexByte("bac", 97)
	fmt.Println(I2)
}
//命令行输入运行go文件的命令,比如:go run test.go,得到输出:
[231 172 168 233 184 159 229 183 165 229 133 183 239 188 140 120 49 121 49 122 49 46 99 111 109]
0
1

实例代码解析

[]byte()方法将字符串str转换为byte数组,其中的元素值与字符串上的字符相对应,是字符的byte值,比如231与“笨”相对应。



全栈后端 / go语法 :













Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.