Home > Backend Development > Golang > Use the strings.Contains function in the Go language document to determine the string inclusion relationship

Use the strings.Contains function in the Go language document to determine the string inclusion relationship

WBOY
Release: 2023-11-04 08:50:28
Original
961 people have browsed it

Use the strings.Contains function in the Go language document to determine the string inclusion relationship

Use the strings.Contains function in the Go language document to determine the string containment relationship

Title: Example of using the strings.Contains function to determine the string containment relationship in the Go language

In the Go language, string processing is one of the common operations. Determining whether a string contains another string is also a frequently encountered need. The Go language provides the strings package, in which the Contains function can be used to determine whether a string contains another string. The following will introduce in detail how to use the strings.Contains function to determine the inclusion relationship of strings.

First, we need to import the strings package:

import "strings"
Copy after login

Next, we can use the Contains function to determine the string inclusion relationship. The Contains function is called as follows:

func Contains(s, substr string) bool
Copy after login

Among them, s represents the string to be searched, and substr represents the substring to be found. The return value of the function is a bool type, indicating whether s contains substr.

Next, let’s look at a specific example. Suppose we have a string s, and we want to determine whether the string contains the substring "hello":

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "hello world"

    if strings.Contains(s, "hello") {
        fmt.Println("字符串s包含子字符串"hello"")
    } else {
        fmt.Println("字符串s不包含子字符串"hello"")
    }
}
Copy after login

In the above code , we use the Contains function to determine whether the string s contains the substring "hello". If included, print "String s contains the substring "hello""; otherwise, print "String s does not contain the substring "hello"".

Run the above code, the output result is "String s contains the substring "hello"", because the string "hello world" contains the substring "hello".

In addition to determining a single substring, we can also determine whether a string contains multiple substrings by calling the Contains function multiple times. For example, we have a string s, and we want to determine whether the string contains both the substrings "hello" and "world":

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "hello world"

    if strings.Contains(s, "hello") && strings.Contains(s, "world") {
        fmt.Println("字符串s同时包含子字符串"hello"和"world"")
    } else {
        fmt.Println("字符串s不同时包含子字符串"hello"和"world"")
    }
}
Copy after login

In the above code, we call the Contains function multiple times to Determine whether the string s contains both the substrings "hello" and "world". If both are included, print "String s contains both the substrings "hello" and "world""; otherwise, print "String s does not contain both the substrings "hello" and "world"".

Run the above code, the output result is "String s contains both the substrings "hello" and "world"", because the string "hello world" contains both the substrings "hello" and "world" .

Through the above example, we can see that using the strings.Contains function in the Go language can easily determine whether a string contains other strings. In actual development, we can flexibly use this function according to the actual situation to complete the judgment of the inclusion relationship of strings.

The above is the detailed content of Use the strings.Contains function in the Go language document to determine the string inclusion relationship. 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