
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
IndexAny Function in Golang
strings.IndexAny is a built-in function in Golang which is used to get the index of the first instance of any Unicode code point from the input substring. If the substring is found, it returns the position starting from 0; else it returns -1.
Syntax
func IndexAny(s, chars string) int
Where,
- s – The original given string.
- chars – It is the substring that is to be checked in the given string.
Example 1
Take a look at the following example.
package main import ( "fmt" "strings" ) func main() { // Defining the Variables var str string var charstring string var text int // Intializing the Strings str = "IndexAny String Function" charstring = "Hyderabad" // Using the IndexAny Function text = strings.IndexAny(str, charstring) // Display the Strings fmt.Println("Given String:", str) fmt.Println("Substring:", charstring) // Output of IndexAny fmt.Println("IndexAny of Substring characters:", text) }
Output
It will generate the following output −
Given String: IndexAny String Function Substring: Hyderabad IndexAny of Substring characters: 2
Observe that the character "d" in the substring appears at the index "2" in the given string. Hence, IndexAny() returns "2".
Example 2
Let us take another example −
package main import ( "fmt" "strings" ) func main() { // Initializing the Strings x := "IndexAny String Function" y := "Golang Strings Package" // Display the Strings fmt.Println("First String:", x) fmt.Println("Second String:", y) // Using the IndexAny Function result1 := strings.IndexAny(x, "net") result2 := strings.IndexAny(x, "lp") result3 := strings.IndexAny(y, "Language") result4 := strings.IndexAny(y, "go") // Display the IndexAny Output fmt.Println("IndexAny of 'net' in the 1st String:", result1) fmt.Println("IndexAny of 'lp' in the 1st String:", result2) fmt.Println("IndexAny of 'Language' in the 2nd String:", result3) fmt.Println("IndexAny of 'go' in the 2nd String:", result4) }
Output
It will generate the following output −
First String: IndexAny String Function Second String: Golang Strings Package IndexAny of 'net' in the 1st String: 1 IndexAny of 'lp' in the 1st String: -1 IndexAny of 'Language' in the 2nd String: 3 IndexAny of 'go' in the 2nd String: 1
Notice that the characters "l" and "p" in the given substring is not present in the 1st string, hence IndexAny() returns "-1".
Advertisements