strings.IndexAny() Function in Golang With Examples
Last Updated :
19 May, 2020
strings.IndexAny() Function in Golang is used to returns the index of the first instance of any Unicode code point from chars in the original string. If the Unicode code point from chars is not available in the original string, then this method will return -1.
Syntax:
func IndexAny(str, charstr string) int
Here, str is the original string and charstr is a Unicode code point from chars whose we want to find index value.
Example 1:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "GeeksforGeeks - A Computer Science Portal"
str2 := "GFG is the Best"
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2: " , str2)
res1 := strings.IndexAny(str1, "G" )
res2 := strings.IndexAny(str2, "Be" )
res3 := strings.IndexAny( "GFG, geeks" , "uywq" )
fmt.Println( "\nIndex values:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2: " , res2)
fmt.Println( "Result 3: " , res3)
}
|
Output:
String 1: GeeksforGeeks - A Computer Science Portal
String 2: GFG is the Best
Index values:
Result 1: 0
Result 2: 9
Result 3: -1
Example 2:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexAny( "Why GFG?" , "F" ))
}
|
Output:
5
Share your thoughts in the comments
Please Login to comment...