VOOZH about

URL: https://www.geeksforgeeks.org/go-language/how-to-find-the-index-value-of-specified-string-in-golang/

⇱ How to find the index value of specified string in Golang? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to find the index value of specified string in Golang?

Last Updated : 12 Jul, 2025
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: 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: 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: 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
Comment
Article Tags:

Explore