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 »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.