ADVERTISEMENT
ADVERTISEMENT

Golang strings.IndexAny() Function with Examples

Golang | strings.IndexAny() Function: Here, we are going to learn about the IndexAny() function of the strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 16, 2021

strings.IndexAny()

The IndexAny() function is an inbuilt function of strings package which is used to get the index of the first occurrence (instance) of any Unicode code point from substring in the string. It accepts two parameters – string and substring (chars) and returns the index, or -1 if no Unicode code point from substring is not present in the string.

Syntax:

func IndexAny(str, substr string) int

Parameter(s):

  • str : String in which we have to check the Unicode code point from the substring.
  • substr : Substring to be checked in the string.

Return Value:

The return type of IndexAny() function is an int, it returns the index of the first occurrence (instance) of any Unicode code point from substring in the string, or -1 if no Unicode code point from substring is present in the string.

Example 1:

// Golang program to demonstrate the
// example of strings.IndexAny() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.IndexAny("Hello, world", "aeoiu"))
	fmt.Println(strings.IndexAny("Hmmm", "aeoiu"))
	fmt.Println(strings.IndexAny("Okay", "oy"))
}

Output:

1
-1
3

Example 2:

// Golang program to demonstrate the
// example of strings.IndexAny() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var substr string
	var index int

	str = "Hello, world!"
	substr = "aeoiu"

	index = strings.IndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Any chars from %q found in %q at %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello, world!"
	substr = "pqx"

	index = strings.IndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Any chars from %q found in %q at %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello! Hi! How are you?"
	substr = "@!?"

	index = strings.IndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Any chars from %q found in %q at %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello! Hi! How are you?"
	substr = ".<>*&"

	index = strings.IndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Any chars from %q found in %q at %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}
}

Output:

Any chars from "aeoiu" found in "Hello, world!" at 1 index.
No chars from "pqx" does not found in "Hello, world!".
Any chars from "@!?" found in "Hello! Hi! How are you?" at 5 index.
No chars from ".<>*&" does not found in "Hello! Hi! How are you?".

Golang strings Package Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.