strings.IndexFunc() Function in Golang With Examples

strings.IndexFunc() Function in Golang is used to returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.

Syntax:

func IndexFunc(str string, f func(rune) bool) int

Here, str is string which may contain Unicode code point and f is func which validates the Unicode point.

Return Value: It returns the index of the first Unicode code point satisfying func.

Example 1:



// Golang program to show the usage
// of strings.IndexFunc() Function
package main

// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)

func main() {

    // func f which validates the Greek 
    // Unicode character in the string
    f := func(c rune) bool {
        return unicode.Is(unicode.Greek, c)
    }
    
    // using the function
    fmt.Println(strings.IndexFunc("Hello Geeks!α", f)) 
}

Output:

13

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show the usage
// of strings.IndexFunc() Function
package main
  
// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)
  
func main() {
  
    // func f which validates the Greek 
    // Unicode character in the string
    f := func(c rune) bool {
        return unicode.Is(unicode.Greek, c)
    }
      
    // using the function
    fmt.Println(strings.IndexFunc("GeeksforGeeks", f)) 
}

chevron_right


Output:

-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.