当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Golang strings.LastIndexByte()用法及代码示例

strings.LastIndexByte() Golang中的函数返回原始字符串中给定字节的最后一个实例的索引。如果给定的字节在原始字符串中不可用,则此方法将返回-1。

用法:

func LastIndexByte(str string, b byte) int

在这里,str是原始字符串,b是一个字节,我们要查找其最后一个索引值。

返回值:它返回整数。

范例1:



// Golang program to illustrate the 
// strings.LastIndexByte() Function 
package main 
  
// importing fmt and strings 
import ( 
    "fmt"
    "strings"
) 
  
// calling main method 
func main() { 
  
    // returns the last index of 'g' in string. 
    fmt.Println(strings.LastIndexByte("geeksforgeeks", 'g')) 
  
    // returns the last index of 'k' in string. 
    fmt.Println(strings.LastIndexByte("geekgeekgeek", 'k')) 
}

输出:

8
11

范例2:

// Golang program to illustrate the 
// strings.LastIndexByte() Function 
package main 
  
// importing fmt and strings 
import ( 
    "fmt"
    "strings"
) 
  
// calling main method 
func main() { 
  
    // performs case-insensitive search(returns -1). 
    fmt.Println(strings.LastIndexByte("GeeksForGeeks", 'g')) 
  
    // performs case-insensitive search(returns -1). 
    fmt.Println(strings.LastIndexByte("GeeKGeeKGeeK", 'k')) 
}

输出:

-1
-1



相关用法


注:本文由纯净天空筛选整理自PranchalKatiyar大神的英文原创作品 strings.LastIndexByte() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。