English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In der Go-Sprache können Sie die folgenden Funktionen verwenden, um den ersten Indexwert eines gegebenen Strings im ursprünglichen String zu finden. Diese Funktionen sind im String-Paket definiert, daher müssen Sie das String-Paket in Ihrem Programm importieren, um diese Funktionen zu verwenden:
1.Index:dient dazu, den Indexwert der ersten Instanz eines gegebenen Strings im ursprünglichen String zu finden. Wenn der gegebene String im ursprünglichen String nicht vorhanden ist, gibt diese Methode-1。
语法:
func Index(str, sbstr string) int
在这里,str是原始字符串,sbstrist der String, den wir als Indexwert finden möchten. Lassen Sie uns mit einem Beispiel diese Konzeption diskutieren:
//给定字符串的索引值 package main import ( "fmt" "strings" ) func main() { //创建和初始化字符串 str1 := "Welcome to the online portal of w3codebox" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //显示字符串 fmt.Println("字符串 1: ", str1) fmt.Println("字符串 2: ", str2) fmt.Println("字符串 3: ", str3) //um den Indexwert eines gegebenen Strings zu finden //Verwenden Sie die Index() -Funktion res1 := strings.Index(str1, "Geeks") res2 := strings.Index(str2, "do") res3 := strings.Index(str3, "chess") res4 := strings.Index("w3codebox, geeks", "ks") //显示结果 fmt.Println("\n索引值:") fmt.Println("结果 1: ", res1) fmt.Println("结果 2: ", res2) fmt.Println("结果 3: ", res3) fmt.Println("结果 4: ", res4) }
输出:
字符串 1Welcome to the online portal of w3codebox 字符串 2My dog name is Dollar 字符串 3I like to play Ludo 索引值: Ergebnis 1: -1 Ergebnis 2: 3 Ergebnis 3: -1 Ergebnis 4: 10
2. IndexAny:Diese Methode gibt den Index der ersten Instanz jedes Unicode-Zeichens zurück, das im ursprünglichen String vorkommt. Wenn im ursprünglichen Zeichen keine Unicode-Zeichencodepunkte aus chars vorhanden sind, gibt diese Methode-1。
语法:
func IndexAny(str, charstr string) int
在这里,str是原始字符串,charstr是chars的Unicode代码点,我们想要查找索引值。
//给定字符串的索引值 package main import ( "fmt" "strings" ) func main() { //创建和初始化字符串 str1 := "Welcome to the online portal of oldtoolbag.com" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" //显示字符串 fmt.Println("字符串 1: ", str1) fmt.Println("字符串 2: ", str2) fmt.Println("字符串 3: ", str3) //查找给定的字符串索引值 //使用IndexAny()函数 res1 := strings.IndexAny(str1, "G") res2 := strings.IndexAny(str2, "do") res3 := strings.IndexAny(str3, "lqxa") res4 := strings.IndexAny("w3codebox, geeks", "uywq") //显示结果 fmt.Println("\n索引值:") fmt.Println("结果 1: ", res1) fmt.Println("结果 2: ", res2) fmt.Println("结果 3: ", res3) fmt.Println("结果 4: ", res4) }
输出:
字符串 1Welcome to the online portal of oldtoolbag.com 字符串 2My dog name is Dollar 字符串 3I like to play Ludo 索引值: Ergebnis 1: -1 Ergebnis 2: 3 Ergebnis 3: 2 Ergebnis 4: -1
3. IndexByte:此函数返回原始字符串中给定字节的第一个实例的索引。如果给定的字节在原始字符串中不存在,则此方法将返回-1。
语法:
func IndexByte(str string, b byte) int
在这里,str是原始字符串,b是一个字节,我们要查找其索引值。让我们借助示例来讨论这个概念:
// 给定字节的索引值 package main import ( "fmt" "strings" ) // Main function func main() { //创建和初始化字符串 str1 := "Welcome to the online portal of oldtoolbag.com" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // 显示字符串 fmt.Println("字符串 1: ", str1) fmt.Println("字符串 2: ", str2) fmt.Println("字符串 3: ", str3) //查找给定字节的索引值 //使用IndexByte()函数 res1 := strings.IndexByte(str1, 'c') res2 := strings.IndexByte(str2, 'o') res3 := strings.IndexByte(str3, 'q') res4 := strings.IndexByte("w3codebox, geeks", 'G') //显示结果 fmt.Println("\n索引值:") fmt.Println("结果 1: ", res1) fmt.Println("结果 2: ", res2) fmt.Println("结果 3: ", res3) fmt.Println("结果 4: ", res4) }
输出:
字符串 1Welcome to the online portal of w3codebox 字符串 2My dog name is Dollar 字符串 3I like to play Ludo 索引值: Ergebnis 1: 3 Ergebnis 2: 4 Ergebnis 3: -1 Ergebnis 4: 0