ContainsAny函数用于检查字符串是否存在字符中的任何 Unicode 代码点。它是用来发现,如果一旦发现,它将返回true否则为false字符串中存在指定的字符串的内置函数。
句法:
func ContainsAny(str, charstr string) bool
这里,第一参数是原始字符串,第二个参数是子串或字符集的哪一个要在字符串内找到。即使在子的字符之一字符串中被发现,该函数返回true。该函数根据输入返回一个布尔值,即真/假。
例子:
// Go program to illustrate how to check whether
// the string is present or not in the specified string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing strings
str1 := "Welcome to Geeks for Geeks"
str2 := "We are here to learn about go strings"
// Checking the string present or
// not using the ContainsAny() function
res1 := strings.ContainsAny(str1, "Geeks")
res2 := strings.ContainsAny(str2, "GFG")
res3 := strings.ContainsAny("GeeksforGeeks", "Gz")
res4 := strings.ContainsAny("GeeksforGeeks", "ue")
res5 := strings.ContainsAny("GeeksforGeeks", " ")
// Displaying the output
fmt.Println("\nResult 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
fmt.Println("Result 4: ", res4)
fmt.Println("Result 5: ", res5)
}
输出:
Result 1: true
Result 2: false
Result 3: true
Result 4: true
Result 5: false