strings.IndexRune() Function in Golang With Examples
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:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexRune( "This is GeeksForGeeks" , 'G' ))
fmt.Println(strings.IndexRune( "This is GeeksForGeeks" , 'y' ))
}
|
Output:
8
-1
Example 2:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexRune( "This is GeeksForGeeks" , 'g' ))
fmt.Println(strings.IndexRune( "This is GeeksForGeeks" , ' ' ))
}
|
Output:
-1
4
Last Updated :
17 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...