Open In App

strings.ContainsAny() Function in Golang with Examples

Last Updated : 08 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

ContainsAny function is used to check whether any Unicode code points in chars are present in the string. It is an inbuilt function used to find if a specified string exists in the string if found it will return true or else false.

Syntax:

func ContainsAny(str, charstr string) bool

Here, the first parameter is the original string and the second parameter is the substring or a set of characters which is to be found within the string. Even if one of the characters in the substring is found in the string, the function returns true. The function returns a boolean value i.e. true/false depending on the input.

Example:




// 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)
  
}


Output:

Result 1:  true
Result 2:  false
Result 3:  true
Result 4:  true
Result 5:  false

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads