Home > Backend Development > Golang > golang finds substring position

golang finds substring position

WBOY
Release: 2023-05-13 10:15:07
Original
1072 people have browsed it

golang is a programming language with very high efficiency and performance, and string operations are no exception. If you need to find the substring position of a string in golang, you can use the functions provided by the strings package in the standard library.

The following are several methods for finding the position of substrings in golang:

1. strings.Index function

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello, world"
    substr := "world"
    index := strings.Index(str, substr)

    fmt.Println("substr in str:", index)
}
Copy after login

Running results:

substr in str: 7
Copy after login

strings.Index The function returns the position of the first occurrence of the substring in the string. If the substring is not in the string, it returns -1.

2. strings.LastIndex function

strings.LastIndex function is similar to the strings.Index function, but it searches for substrings starting from the end of the string. string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello, world, world"
    substr := "world"
    index := strings.LastIndex(str, substr)

    fmt.Println("substr in str:", index)
}
Copy after login

Run result:

substr in str: 13
Copy after login

3. strings.IndexAny function

strings.IndexAny The function is used to find any character in the substring. The position of the first occurrence in the string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello, world"
    substr := "ow"
    index := strings.IndexAny(str, substr)

    fmt.Println("substr in str:", index)
}
Copy after login

Run result:

substr in str: 4
Copy after login

4. strings.IndexByte function

strings.IndexByte The function is used to find the specified byte in the substring. The position of the first occurrence in the string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello, world"
    substr := ','
    index := strings.IndexByte(str, substr)

    fmt.Println("substr in str:", index)
}
Copy after login

Run result:

substr in str: 5
Copy after login

5. strings.IndexFunc function

strings.IndexFunc The function is used to find characters in a substring that meet specified conditions. The position of the first occurrence in the string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello, world"
    substr := func(r rune) bool {
        return r == 'l'
    }
    index := strings.IndexFunc(str, substr)

    fmt.Println("substr in str:", index)
}
Copy after login

Run results:

substr in str: 2
Copy after login

The above are several methods for finding the substring position in golang. Choosing the appropriate method according to different situations can effectively improve the efficiency of the program.

The above is the detailed content of golang finds substring position. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template