Home  >  Article  >  Backend Development  >  Use the strings.IndexAny function to return the first occurrence of a specified character set in a string

Use the strings.IndexAny function to return the first occurrence of a specified character set in a string

王林
王林Original
2023-07-25 20:25:101544browse

Title: Detailed explanation of the use of strings.IndexAny function in Golang

In the Golang programming language, the strings package provides many functions for processing strings. Among them, the strings.IndexAny function is widely used to find the first occurrence of a specified character set in a string. This article will introduce the usage of the strings.IndexAny function in detail and provide some examples to help readers better understand its application scenarios.

1. Function overview

The strings.IndexAny function is defined as follows:

func IndexAny(s, chars string) int

It accepts two parameters : s is the string to be searched, and chars is the specified character set. The return value of the function is the index position of the first character in the string s in the specified character set. If no characters are found, -1 is returned.

2. Function usage examples

The following uses several examples to illustrate the use of the strings.IndexAny function.

Example 1:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello World!"
    charset := "abcde"
    index := strings.IndexAny(str, charset)
    fmt.Println(index)
}

Running result:

-1

Explanation: In the string "Hello World!", no characters in the character set "abcde" were found. Therefore -1 is returned.

Example 2:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello World!"
    charset := "lo"
    index := strings.IndexAny(str, charset)
    fmt.Println(index)
}

Running result:

3

Explanation: In the string "Hello World!", the first character in the character set "lo" appears The character is 'l' and its index position is 3.

Example 3:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello World!"
    charset := "lll"
    index := strings.IndexAny(str, charset)
    fmt.Println(index)
}

Run result:

2

Explanation: In the string "Hello World!", the first character in the character set "lll" appears The character is 'l' and its index position is 2.

3. Notes

When multiple characters in a string belong to the character set, strings.IndexAny only returns the index of the first occurrence. If you need to find and return all indexes that meet the criteria, you can use strings.IndexRune, a variant of the strings.Index function.

4. Summary

This article introduces in detail the usage and examples of the strings.IndexAny function in Golang. By using this function, we can find the first occurrence of a specified set of characters in a string. In the actual programming process, this function can be reasonably applied according to needs to improve the efficiency of string processing. Hope this article is helpful to readers.

The above is the detailed content of Use the strings.IndexAny function to return the first occurrence of a specified character set in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn