Skip to content
Related Articles

Related Articles

Improve Article
How to find the index value of specified string in Golang?
  • Last Updated : 26 Aug, 2019

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :