In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In Go strings, you can find the first index value of the specified string from the original string using the following function. These functions are defined under strings package so, you have to import strings package in your program for accessing these functions:
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 is not available in the original string, then this method will return -1.
Syntax:
func Index(str, sbstr string) int
Here, str is the original string and sbstr is a string whose we want to find index value. Let us discuss this concept with the help of an example:
Example:
// Go program to illustrate how to find // the index value of the given string package main import ( "fmt" "strings") // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of GeeksforGeeks" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // Displaying strings fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // Finding the index value of the given strings // Using Index() function res1 := strings.Index(str1, "Geeks") res2 := strings.Index(str2, "do") res3 := strings.Index(str3, "chess") res4 := strings.Index("GeeksforGeeks, geeks", "ks") // Displaying the result fmt.Println("\nIndex values:") fmt.Println("Result 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) } |
Output:
String 1: Welcome to the online portal of GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 32 Result 2: 3 Result 3: -1 Result 4: 3
2. IndexAny: This method returns the index of the first instance of any Unicode code point from chars in the original string. If the Unicode code point from chars is not available in the original string, then this method will return -1.
Syntax:
func IndexAny(str, charstr string) int
Here, str is the original string and charstr is a Unicode code point from chars whose we want to find index value.
Example:
// Go program to illustrate how to find // the index value of the given string package main import ( "fmt" "strings") // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of GeeksforGeeks" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // Displaying strings fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // Finding the index value // of the given strings // Using IndexAny() function res1 := strings.IndexAny(str1, "G") res2 := strings.IndexAny(str2, "do") res3 := strings.IndexAny(str3, "lqxa") res4 := strings.IndexAny("GeeksforGeeks, geeks", "uywq") // Displaying the result fmt.Println("\nIndex values:") fmt.Println("Result 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) } |
Output:
String 1: Welcome to the online portal of GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 32 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 is not available in the original string, then this method will return -1.
Syntax:
func IndexByte(str string, b byte) int
Here, str is the original string and b is a byte, whose we want to find the index value. Let us discuss this concept with the help of an example:
Example:
// Go program to illustrate how to find // the index value of the given bytes package main import ( "fmt" "strings") // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of GeeksforGeeks" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // Displaying strings fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // Finding the index value of the given bytes // Using IndexByte() function res1 := strings.IndexByte(str1, 'c') res2 := strings.IndexByte(str2, 'o') res3 := strings.IndexByte(str3, 'q') res4 := strings.IndexByte("GeeksforGeeks, geeks", 'G') // Displaying the result fmt.Println("\nIndex values:") fmt.Println("Result 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) } |
Output:
String 1: Welcome to the online portal of GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 3 Result 2: 4 Result 3: -1 Result 4: 0
Recommended Posts:
- How to find the last index value of specified string in Golang?
- How to find last index value of specified byte in slice of bytes in Golang?
- Finding the index value of specified byte in slice of bytes in Golang
- Finding Absolute Value For Specified Complex Number in Golang?
- Finding the Absolute Value of Specified Number in Golang
- Finding Cosine Value of Specified Number in Golang
- Finding Sine Value of Specified Number in Golang
- Finding the Ceiling Value of Specified Number in Golang
- Finding the Integer Value of Specified Number in Golang
- Check if the String starts with specified prefix in Golang
- Check if the String ends with specified suffix in Golang
- How to check the specified rune in Golang String?
- Golang | Checking the string for the specified regular expression
- Golang | Splitting the string after the specified separator
- How to Get the String in Specified Base in Golang?
- How to find the first index value in slice of bytes in Golang?
- How to find the last index value in slice of bytes in Golang?
- How to find the index value of any element in slice of bytes in Golang?
- How to find the last index value of any element in slice of bytes in Golang?
- Check if the specified element is present in the slice of bytes in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

