go strings.IndexRune()方法

strings.IndexRune()方法

Golang的strings.IndexRune()方法,可以查找Unicode编码值r在字符串s中第一次出现的位置索引,如果不存在,则返回-1。字符对应的Unicode编码值,可参考:unicode编码转换工具在线


语法

func IndexRune(s string, r rune) int

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

IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.

参数

参数描述
sstring字符串类型
rgo rune类型,与int32等价

返回值

Unicode编码值对应的字符在字符串中的索引位置,或-1。


strings.IndexRune()方法实例代码

func main() {
	str1 := "笨鸟工具,x1y1z1.com	"
	var index1 int = strings.IndexRune(str1, 31528)
	fmt.Println(index1)
}
//命令行输入运行go文件的命令,比如:go run test.go,得到输出:
0

实例代码解析

31528是”笨“字对应的Unicode编码值。



全栈后端 / go语法 :













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