Golang strings.LastIndexFunc()函数及示例
strings.LastIndexFunc() Function 在Golang中返回字符串s中最后一个满足func(c)条件的Unicode码点的索引,如果没有找到,则返回-1。
语法:
func LastIndexFunc(s string, f func(rune) bool) int
这里,s是字符串,func()是函数。
返回值: 返回整数。
示例1:
//演示strings.LastIndexFunc()函数的Golang程序
package main
//导入fmt、strings和unicode包
import (
"fmt"
"strings"
"unicode"
)
//调用主方法
func main() {
//返回数字的最后一个索引
fmt.Println(strings.LastIndexFunc("GeeksForGeeks123", unicode.IsNumber))
//返回数字的最后一个索引。
fmt.Println(strings.LastIndexFunc("23Geeks1", unicode.IsNumber))
}
输出:
15
7
示例2:
//演示strings.LastIndexFunc()函数的Golang程序
package main
//导入fmt、strings和unicode包
import (
"fmt"
"strings"
"unicode"
)
//调用主方法
func main() {
//返回字母的最后一个索引
fmt.Println(strings.LastIndexFunc("GeeksForGeeks123", unicode.IsLetter))
//返回字母的最后一个索引
fmt.Println(strings.LastIndexFunc("23Geeks1", unicode.IsLetter))
}
输出:
12
6