How to find the index value of specified string in Golang?
Last Updated :
26 Aug, 2019
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:
C
// Go program to illustrate how to find
// the index value of the given string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing the strings
str1 := "Welcome to the online portal of GeeksforGeeks"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"
// Displaying strings
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
fmt.Println("String 3: ", str3)
// Finding the index value of the given strings
// Using Index() function
res1 := strings.Index(str1, "Geeks")
res2 := strings.Index(str2, "do")
res3 := strings.Index(str3, "chess")
res4 := strings.Index("GeeksforGeeks, geeks", "ks")
// Displaying the result
fmt.Println("\nIndex values:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
fmt.Println("Result 4: ", res4)
}
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:
C
// Go program to illustrate how to find
// the index value of the given string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing the strings
str1 := "Welcome to the online portal of GeeksforGeeks"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"
// Displaying strings
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
fmt.Println("String 3: ", str3)
// Finding the index value
// of the given strings
// Using IndexAny() function
res1 := strings.IndexAny(str1, "G")
res2 := strings.IndexAny(str2, "do")
res3 := strings.IndexAny(str3, "lqxa")
res4 := strings.IndexAny("GeeksforGeeks, geeks", "uywq")
// Displaying the result
fmt.Println("\nIndex values:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
fmt.Println("Result 4: ", res4)
}
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:
C
// Go program to illustrate how to find
// the index value of the given bytes
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing the strings
str1 := "Welcome to the online portal of GeeksforGeeks"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"
// Displaying strings
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
fmt.Println("String 3: ", str3)
// Finding the index value of the given bytes
// Using IndexByte() function
res1 := strings.IndexByte(str1, 'c')
res2 := strings.IndexByte(str2, 'o')
res3 := strings.IndexByte(str3, 'q')
res4 := strings.IndexByte("GeeksforGeeks, geeks", 'G')
// Displaying the result
fmt.Println("\nIndex values:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
fmt.Println("Result 4: ", res4)
}
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
Similar Reads
Golang | How to find the index of rune in the string? 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 the Go strings, you can also find the first index of the specified rune in
3 min read
How to check the specified rune in Golang String? 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 the Go strings, you are allowed to check the given string contain the spec
3 min read
How to Get the String in Specified Base in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
2 min read
How to find the last index value in slice of bytes in Golang? In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. The LastIndex() function from the bytes package returns the last index of a specified value in a byte slice, or -1 if the value is not found.Examplepackage mainimport ( "bytes" "fmt")func
3 min read
How to find the type of Struct in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support
3 min read