strings.IndexByte() Function in Golang With Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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'))
}

chevron_right


Output:

0
8

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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'))
}

chevron_right


Output:

-1
-1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.