strings.IndexAny() Function in Golang With Examples
Last Updated :
19 May, 2020
Improve
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:
// Golang program to illustrate // the strings.IndexAny() Function package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "GeeksforGeeks - A Computer Science Portal" str2 := "GFG is the Best" // Displaying strings fmt.Println( "String 1: " , str1) fmt.Println( "String 2: " , str2) // Finding the index value // of the given strings // Using IndexAny() function res1 := strings.IndexAny(str1, "G" ) res2 := strings.IndexAny(str2, "Be" ) res3 := strings.IndexAny( "GFG, geeks" , "uywq" ) // Displaying the result 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:
// Golang program to illustrate // the strings.IndexAny() Function package main import ( "fmt" "strings" ) // Main function func main() { // using the function fmt.Println(strings.IndexAny( "Why GFG?" , "F" )) } |
Output:
5