OneBite.Dev - Coding blog in a bite size

find the index of a substring within a string in Go

Code snippet on how to find the index of a substring within a string in Go

  i := strings.Index(s, substr)

This code looks for the index of a substring, “substr”, within a string, “s”. The variable “i” is used to store the result which is an integer representing the index of the substring within the string. The strings package from the Go standard library is used, which contains a function, Index, that is used to find the index of the substring in the string. If the substring does not exist in the string, the Index function will return -1.

go