go LastIndex()方法

LastIndex()语法

func LastIndex(s, sep string) int

go源码对LastIndex()方法的介绍:

LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.

即,golang的标准库strings的LastIndex()方法,可以用于查找sep子串在字符串s中最后一次出现的位置的索引,如果sep不在s字符串中,则返回-1。


LastIndex()方法实例代码

一般情况下,该方法需要通过strings来调用:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str1 := "笨鸟工具,x1y1z1.com"
	var i int = strings.LastIndex(str1, "1")
	fmt.Println(i)
}

运行go文件,得到输出:

20

全栈后端 / go语法 :













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