ContainsAny函數用於檢查字符串中是否存在char中的任何Unicode代碼點。它是一個內置函數,用於查找字符串中是否存在指定的字符串(如果找到),則它將返回true或false。
用法:
func ContainsAny(str, charstr string) bool
在這裏,第一個參數是原始字符串,第二個參數是子字符串或在字符串中可以找到的一組字符。即使在字符串中找到子字符串中的字符之一,該函數也會返回true。該函數返回一個布爾值,即true /false(取決於輸入)。
例:
// 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
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自rachnasoundatti3099大神的英文原創作品 strings.ContainsAny() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。