Golang 如何在字符串中找到符文的索引
在Go语言中,字符串与其他语言如Java、C++、Python等不同。它是一个可变宽度的字符序列,其中每个字符都由一个或多个使用UTF-8编码的字节表示。
在Go语言的字符串中,你也可以使用 IndexRune() 函数找到指定符文在指定字符串中的第一个索引。该函数返回Unicode编码点的第一个实例的索引,即指定的符文,如果指定的符文不存在于给定的字符串中,则返回-1。如果符文是utf8.RuneError ,那么它返回任何无效的UTF-8字节序列的第一个实例。它被定义在string包中,所以你必须在你的程序中导入string包以访问IndexRune函数。
语法
func IndexRune(str string, r rune) int
例1 :
// Go program to illustrate how to find
// the index value of the given rune
package main
import (
"fmt"
"strings"
)
func main() {
// Creating and Finding the first index
// of the rune in the given string
// Using IndexRunefunction
res1 := strings.IndexRune("****Welcome to GeeksforGeeks****", 60)
res2 := strings.IndexRune("Learning how to trim"+
" a slice of bytes", 'r')
res3 := strings.IndexRune("GeeksforGeeks", 'G')
// Display the results
fmt.Println("Index Value 1: ", res1)
fmt.Println("Index Value 2: ", res2)
fmt.Println("Index Value 3: ", res3)
}
输出
Index Value 1: -1
Index Value 2: 3
Index Value 3: 0
例2 :
// Go program to illustrate how to find
// the index value of the given rune
package main
import (
"fmt"
"strings"
)
func main() {
// Creating and initializing a string
// Using shorthand declaration
string_1 := "Welcome to GeeksforGeeks"
string_2 := "AppleAppleAppleAppleAppleApple"
string_3 := "%G%E%E%K%S"
// Creating and initializing rune
var r1, r2, r3 rune
r1 = 'R'
r2 = 'l'
r3 = 42
// Finding the first index
// of the given rune
// Using IndexRune function
res1 := strings.IndexRune(string_1, r1)
res2 := strings.IndexRune(string_2, r2)
res3 := strings.IndexRune(string_3, r3)
// Display the results
fmt.Printf("String 1: %s , Rune 1:%q , Index Value: %d",
string_1, r1, res1)
fmt.Printf("\nString 2: %s , Rune 2:%q , Index Value: %d",
string_2, r2, res2)
fmt.Printf("\nString 3: %s , Rune 3:%q , Index Value: %d",
string_3, r3, res3)
}
输出
String 1: Welcome to GeeksforGeeks , Rune 1:'R' , Index Value: -1
String 2: AppleAppleAppleAppleAppleApple , Rune 2:'l' , Index Value: 3
String 3: %G%E%E%K%S , Rune 3:'*' , Index Value: -1
极客教程