字符串.IndexFunc() Golang 中的函数用于返回第一个满足 f(c) 的 Unicode 代码点的 s 的索引,如果没有,则返回 -1。
句法:
func IndexFunc(str string, f func(rune) bool) int
这里,str 是可能包含 Unicode 代码点的字符串,f 是验证 Unicode 点的 func。
返回值:返回第一个满足 func 的 Unicode 代码点的索引。
示例 1:
// Golang program to show the usage
// of strings.IndexFunc() Function
package main
// importing fmt, unicode and strings
import (
"fmt"
"strings"
"unicode"
)
func main() {
// func f which validates the Greek
// Unicode character in the string
f := func(c rune) bool {
return unicode.Is(unicode.Greek, c)
}
// using the function
fmt.Println(strings.IndexFunc("Hello Geeks!α", f))
}
输出:
13
示例 2:
// Golang program to show the usage
// of strings.IndexFunc() Function
package main
// importing fmt, unicode and strings
import (
"fmt"
"strings"
"unicode"
)
func main() {
// func f which validates the Greek
// Unicode character in the string
f := func(c rune) bool {
return unicode.Is(unicode.Greek, c)
}
// using the function
fmt.Println(strings.IndexFunc("GeeksforGeeks", f))
}
输出:
-1