Golang strings.LastIndexFunc()函数及示例

Golang strings.LastIndexFunc()函数及示例

strings.LastIndexFunc() Function 在Golang中返回字符串s中最后一个满足func(c)条件的Unicode码点的索引,如果没有找到,则返回-1。

语法:

func LastIndexFunc(s string, f func(rune) bool) int
Go

这里,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))
} 
Go

输出:

15
7
Go

示例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))
} 
Go

输出:

12
6
Go

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册