English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Go language strings, you can use the following functions to find the first index value of the specified string from the original string. These functions are defined under the string package, so you must import the strings package in your program to use these features:
1.Index:This function is used to find the index value of the first instance of the given string from the original string. If the given string does not exist in the original string, this method will return-1.
Syntax:
func Index(str, sbstr string) int
Here,stris the original string,sbstris the string we want to find the index value. Let's discuss this concept with an example:
//The index value of a given string package main import ( "fmt" "strings" ) func main() { //Create and initialize strings str1 := "Welcome to the online portal of w3codebox" str2 := "My dog's name is Dollar" str3 := "I like to play Ludo" //Display string fmt.Println("String" 1: "str"1) fmt.Println("String" 2: "str"2) fmt.Println("String" 3: "str"3) //to find the index value of the given string //Using the Index() function res1 := strings.Index(str1, "Geeks") res2 := strings.Index(str2, "do") res3 := strings.Index(str3, "chess") res4 := strings.Index("w3codebox, geeks, "ks") //Display result fmt.Println("\nIndex value:") fmt.Println("Result" 1: "res"1) fmt.Println("Result" 2: "res"2) fmt.Println("Result" 3: "res"3) fmt.Println("Result" 4: "res"4) }
Output:
String 1: Welcome to the online portal of w3codebox String 2: My dog's name is Dollar String 3: I like to play Ludo Index value: Result 1: -1 Result 2: 3 Result 3: -1 Result 4: 10
2. IndexAny:This method returns the index value of the first Unicode instance from the chars in the original string. If there is no Unicode code point from chars in the original character, this method will return-1.
Syntax:
func IndexAny(str, charstr string) int
Here,stris the original string,charstris the Unicode code point of chars, and we want to find the index value.
//The index value of a given string package main import ( "fmt" "strings" ) func main() { //Create and initialize strings str1 := "Welcome to the online portal of oldtoolbag.com" str2 := "My dog's name is Dollar" str3 := "I like to play Ludo" //Display string fmt.Println("String" 1: "str"1) fmt.Println("String" 2: "str"2) fmt.Println("String" 3: "str"3) //Find the index value of a given string //Using IndexAny() function res1 := strings.IndexAny(str1, "G") res2 := strings.IndexAny(str2, "do") res3 := strings.IndexAny(str3, "lqxa") res4 := strings.IndexAny("w3codebox, geeks", "uywq") //Display result fmt.Println("\nIndex value:") fmt.Println("Result" 1: "res"1) fmt.Println("Result" 2: "res"2) fmt.Println("Result" 3: "res"3) fmt.Println("Result" 4: "res"4) }
Output:
String 1: Welcome to the online portal of oldtoolbag.com String 2: My dog's name is Dollar String 3: I like to play Ludo Index value: Result 1: -1 Result 2: 3 Result 3: 2 Result 4: -1
3. IndexByte:This function returns the index of the first instance of the given byte in the original string. If the given byte does not exist in the original string, this method will return-1.
Syntax:
func IndexByte(str string, b byte) int
Here,stris the original string,bis a byte, and we want to find its index value. Let's discuss this concept with an example:
// Given byte index value package main import ( "fmt" "strings" ) // Main function func main() { //Create and initialize strings str1 := "Welcome to the online portal of oldtoolbag.com" str2 := "My dog's name is Dollar" str3 := "I like to play Ludo" // Display string fmt.Println("String" 1: "str"1) fmt.Println("String" 2: "str"2) fmt.Println("String" 3: "str"3) //Find the index value of a given byte //Using IndexByte() function res1 := strings.IndexByte(str1, 'c') res2 := strings.IndexByte(str2, 'o') res3 := strings.IndexByte(str3, 'q') res4 := strings.IndexByte("w3codebox, geeks //Display result fmt.Println("\nIndex value:") fmt.Println("Result" 1: "res"1) fmt.Println("Result" 2: "res"2) fmt.Println("Result" 3: "res"3) fmt.Println("Result" 4: "res"4) }
Output:
String 1: Welcome to the online portal of w3codebox String 2: My dog's name is Dollar String 3: I like to play Ludo Index value: Result 1: 3 Result 2: 4 Result 3: -1 Result 4: 0