Golang strings.ContainsAny()的使用及示例

Golang strings.ContainsAny()的使用及示例

ContainsAny函数用于检查字符集chars中的任何Unicode代码点是否存在于字符串中。它是一个内置函数,用于查找指定字符串是否存在于字符串中,如果找到,则返回true,否则返回false。

语法:

func ContainsAny(str, charstr string) bool

这里,第一个参数是原始字符串,第二个参数是要在其中查找子字符串或一组字符。即使在字符串中找到子串中的一个字符,该函数也会返回true。函数根据输入返回布尔值即true/false。

示例:

// Go程序示例,演示如何检查特定字符串是否出现在指定字符串中
package main
  
import (
    "fmt"
    "strings"
)
  
// 主函数
func main() {
  
    // 创建和初始化字符串
    str1 := "Welcome to Geeks for Geeks"
    str2 := "We are here to learn about go strings"
      
    // 使用ContainsAny()函数检查字符串是否存在  
    res1 := strings.ContainsAny(str1, "Geeks")
    res2 := strings.ContainsAny(str2, "GFG")
    res3 := strings.ContainsAny("GeeksforGeeks", "Gz")
    res4 := strings.ContainsAny("GeeksforGeeks", "ue")
    res5 := strings.ContainsAny("GeeksforGeeks", " ")
  
    // 显示输出
    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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程