GoLang String - Index()

In this tutorial, we will learn how to get index position of character or sub-string from the given string using in-built method of GO.

In the Go Language, Index() is used to return the index position of particular character or substring present in the actual string. let us explore the uses case with help of simple examples.

String Index()

Index() is the method available in strings package which will return the index position of particular character or substring present in the actual string. If substring not found, then -1 is returned. Indexing starts from 0. Here, it will return the starting position index of the substring.

Syntax:

strings.Index(actual_string,sub_string)

It takes two parameters.

Parameters:

actual_string is the string and sub_string is the string in which its index is returned.

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "welcome to gkindex" and return the index of some characters.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // return the index of "e".
     fmt.Println(strings.Index(actual_string,"e"))
     
     // return the index of "o".
     fmt.Println(strings.Index(actual_string,"o"))
     
     // return the index of "x".
     fmt.Println(strings.Index(actual_string,"x"))
     
     // return the index of "M".
     fmt.Println(strings.Index(actual_string,"M"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
1
4
17
-1

Explanation for the above output:

  1. Return the index of "e". Its position is 1
  2. Return the index of "o". Its position is 4
  3. Return the index of "x". Its position is 17
  4. Return the index of "M". It is not found in the actual_string. So, -1 is returned.

Example 2:

Let us consider the string - "welcome to gkindex" and return the index of some sub-strings.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // return the index of "welcome".
     fmt.Println(strings.Index(actual_string,"welcome"))
     
     // return the index of "to".
     fmt.Println(strings.Index(actual_string,"to"))
     
     // return the index of "index".
     fmt.Println(strings.Index(actual_string,"index"))
     
     // return the index of "python".
     fmt.Println(strings.Index(actual_string,"python"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
0
8
13
-1

Explanation for the above output:

  1. Return the index of "welcome". this substring starts from 0th position.
  2. Return the index of "to". this substring starts from 8th position.
  3. Return the index of "index". this substring starts from 13th position.
  4. Return the index of "python". It is not found in the actual_string. So, -1 is returned.

Conclusion

Now we know how to find index of particular character or substring in the string in the Golang using Index() function and understood the implementation with working examples.