Go Index()
Published Jul 6, 2023
Contribute to Docs
The Index() function returns the index value of the first occurrence of a substring in the original string, otherwise it returns -1 if the substring is not present in the original string.
Syntax
strings.Index(str, sub_str)
Where str is the original string and sub_str is the substring to find the first occurrence in the original string.
Example
The following example demonstrates the use of the strings.Index() function:
package mainimport ("fmt""strings")func main() {fmt.Println(strings.Index("chandler","and"))fmt.Println(strings.Index("navy","as"))}
The output will be:
2-1
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours