Golang Program to get the index of the substring in a string


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. A substring is a portion of a string that contains a sequence of characters from the original string. A substring can be obtained in Go by using the slicing syntax on a string value. In this article we are going to learn different methods to get the index of a substring in a string using golang programming.

Method 1: Using a For Loop

In this method, the function that we create will take both the string and the substring as arguments and will process them by using for loop to return the index of the position at which the given substring is found.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then we need to create the function indexOfString(). This function takes the string and the substring as argument to the function and returns an integer value.

  • Step 3 − Inside this function we are using a for loop to iterate over the string and use a if condition to check whether the current character of substring is present in any of the position of string or not.

  • Step 4 − If the substring is a part of the string then we need to return the value of the iterative variable.

  • Step 5 − If the substring is not a part of string then we need to return -1. Now start the main() function. Inside the main() section of the program initialize two variables naming string and substring of type string and store values in them.

  • Step 6 − Now, call the IndexOfStrings() function by passing both the string and substring as arguments to the function. Store the result in a variable called index.

  • Step 7 − If the value returned by the index is -1 then we need to print that the substring is not a part of string otherwise print that the substring is found along with the index value on the screen.

Example

In this example we will use a for loop to get the index of the substring in a string by using a for loop in an external function.

package main
import "fmt"

func IndexOfSubstring(str, subStr string) int {
   for i := 0; i < len(str); i++ {
      if str[i:i+len(subStr)] == subStr {
         return i
      }
   }
   return -1
}

func main() {
   var str string = "Its a beautiful day"
   var subStr string = "day"
   var index int = IndexOfSubstring(str, subStr)
   if index != -1 {
      fmt.Printf("The substring '%s' is found at index %d in the string '%s'\n", subStr, index, str)
   } else {
      fmt.Printf("The substring '%s' is not found in the string '%s'\n", subStr, str)
   }
}

Output

The substring 'day' is found at index 16 in the string 'Its a beautiful day'

Method 2: Using Index() Function

In this method, we are going to use the internal Index function in golang. In the first example, we are using the index() function and in the second we are using indexRune() function. Both the syntax are explained below −

Syntax

func Index(s, substr string) int

The index() function is present in strings package and is used to get the index of the first occurrence of a given substring. The function takes two values as arguments. One is the string and other is the substring whose occurrence is to be detected. The function then returns the first occurrence of that substring in integer format.

func IndexRune(s, substr string) int

The indexRune() function is present in strings package and is used to get the index of the first occurrence of a given rune character. The function takes two values as arguments. One is the string and other is the rune whose occurrence is to be detected. The function then returns the integer value as the first occurrence of that rune.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Then, we need to start the main() function. Inside the main() initialize two variables called string and substr and assign value to them.

  • Step 3 − Now, call the Index() function by passing the string as well as the substring as arguments to it and store the result in a variable called index.

  • Step 4 − If the value of index is -1 then we need to print that the substring is not a part of string otherwise print that the substring is found along with the index value on the screen.

Example 1

In this example, we will use the Index() function in order to get the index of a substring present in the string.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str string = "Your smile is a precious gem keep smiling!"
   var subStr string = "gem"
   index := strings.Index(str, subStr)
   if index != -1 {
      fmt.Printf("The substring '%s' is found at index %d in the string '%s'\n", subStr, index, str)
   } else {
      fmt.Printf("The substring '%s' is not found in the string '%s'\n", subStr, str)
   }
}

Output

The substring 'gem' is found at index 25 in the string 'Your smile is a precious gem keep smiling!'

Example 2

In this example, we will use the IndexRune() function present in the strings package in order to get the index of a substring in a string.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str string = "Your smile is a precious gem keep smiling!"
   var subStr rune = 'p'
   var index int = strings.IndexRune(str, subStr)
   if index != -1 {
      fmt.Printf("The substring '%c' is found at index %d in the string '%s'\n", subStr, index, str)
   } else {
      fmt.Printf("The substring '%c' is not found in the string '%s'\n", subStr, str)
   }
}

Output

The substring 'p' is found at index 16 in the string 'Your smile is a precious gem keep smiling!'

Conclusion

We have successfully compiled and executed a Golang Program to get the index of the substring in a string along with examples.

Updated on: 17-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements