Skip to content
Related Articles

Related Articles

strings.IndexRune() Function in Golang With Examples
  • Last Updated : 17 May, 2020

strings.IndexRune() Function in Golang is used to find the first index of the specified rune in the given string. It is defined under the string package so, you have to import string package in your program for accessing IndexRune function

Syntax:

func IndexRune(str string, r rune) int

This function returns the index of the first instance of the Unicode code point, i.e, specified rune, or -1 if the specified rune is not present in the given string. If the rune is utf8.RuneError, then it returns the first instance of any invalid UTF-8 byte sequence.

Example 1:




// Golang program to show the usage
// of strings.IndexRune() Function
package main
  
// importing fmt and strings
import (
    "fmt"
    "strings"
)
  
func main() {
  
    // Returns the index of 'G' in string.
    fmt.Println(strings.IndexRune("This is GeeksForGeeks", 'G'))
  
    // Returns -1 because 'y' is not present in string.
    fmt.Println(strings.IndexRune("This is GeeksForGeeks", 'y'))
}

Output:

8
-1

Example 2:




// Golang program to show the usage
// of strings.IndexRune() Function
package main
  
// importing fmt and strings
import (
    "fmt"
    "strings"
)
  
func main() {
  
    // Returns -1 because of case-sensitive matching.
    fmt.Println(strings.IndexRune("This is GeeksForGeeks", 'g'))
  
    // Returns the index of space in string.
    fmt.Println(strings.IndexRune("This is GeeksForGeeks", ' '))
}

Output:

-1
4



My Personal Notes arrow_drop_up
Recommended Articles
Page :