strings.LastIndexAny() Golang中的函數用於從給定字符串中的字符中查找任何Unicode代碼點的最後一個實例的索引。如果找不到來自chars的Unicode代碼點,則返回-1。因此,此函數返回一個整數值。索引以零作為字符串的起始索引進行計數。
用法:
func LastIndexAny(str, chars string) int
在這裏,str是原始字符串,charstr是chars的Unicode代碼點,我們想要找到最後一個索引值。
範例1:
// Golang program to illustrate the
// strings.LastIndexAny() Function
package main
import (
"fmt"
"strings"
)
func main() {
// taking a string
str:= "GeeksforGeeks"
// using the function
fmt.Println(strings.LastIndexAny(str, "Ge"))
fmt.Println(strings.LastIndexAny(str, "g"))
fmt.Println(strings.LastIndexAny(str, "sf"))
}
輸出:
10 -1 12
對於第二個輸出,不存在字符‘g’,因此結果顯示為-1。請注意,此函數區分大小寫,因此對'G'和‘g’的使用會有所不同。
範例2:
// Golang program to illustrate the
// strings.LastIndexAny() Function
package main
import (
"fmt"
"strings"
)
func main() {
// taking a string
str:= "New Delhi, India"
// using the function
fmt.Println(strings.LastIndexAny(str, "Ii"))
fmt.Println(strings.LastIndexAny(str, " "))
}
輸出:
14 10
對於第一個輸出,字符為“ I”和‘i’。因此,編譯器將顯示最後一次出現的“ I”或‘i’的索引,並且由於‘i’排在最後,因此將作為輸出。對於第二個輸出,要搜索的字符是一個空格。由於給定的字符串中有兩個空格,因此輸出將是最後一個空格的索引。
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自prakhar7大神的英文原創作品 strings.LastIndexAny() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。