當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。