strings.IndexByte() Function in Golang 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 1:
// Golang program to illustrate the // strings.IndexByte() Function package main // importing fmt and strings import ( "fmt"
"strings"
) // calling main method func main() { // 'g' present at index 0 of string.
fmt.Println(strings.IndexByte( "geeksforgeeks" , 'g' ))
// 's' present at index 8 of string.
fmt.Println(strings.IndexByte( "computerscience" , 's' ))
} |
Output:
0 8
Example 2:
// Golang program to illustrate the // strings.IndexByte() Function package main // importing fmt and strings import ( "fmt"
"strings"
) // calling main method func main() { // 's' not present in string.
fmt.Println(strings.IndexByte( "computerscience" , 'S' ))
// 'f' not present in string.
fmt.Println(strings.IndexByte( "GFG" , 'f' ))
} |
Output:
-1 -1
Article Tags :