strings.ContainsAny() Function in Golang with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Result 1:  true
Result 2:  false
Result 3:  true
Result 4:  true
Result 5:  false
My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.