The strings
package is a Go standard library package that contains functions to manipulate UTF-8 encoded strings.
ContainsAny()
methodThe strings
package provides the ContainsAny()
method, which can be used to check if the input string contains any of the characters or Unicode code points of another string (passed as another input to the function).
A Unicode code point is an integer value that uniquely identifies a given character. There are different encodings, like UTF-8 or UTF-16, which we can use to encode the Unicode characters. Each character’s Unicode code point can be encoded as one or more bytes specified by the encodings.
func ContainsAny(org_string, sub_string) bool
ContainsAny()
takes two arguments. The first oneorg_string
is the original string and the second sub_string
is a substring or any string that needs to be searched within the original string.true
if any of the Unicode code points of sub_string
is present in org_string
.First, we imported the fmt
and strings
package to our program in the code below:
package mainimport ("fmt""strings")func main() {str1 := "educative.io"fmt.Println(str1, "i", strings.ContainsAny(str1, "i"))fmt.Println(str1, "xf", strings.ContainsAny(str1, "xf"))}
After importing fmt
, we call the ContainsAny()
method with educative.io
as the input string and i
as the chars
list. This returns true as the character i
is present in the string "educative.io"
.
We again call the ContainsAny()
method with educative.io
as the input string and xf
as the chars
list. This returns false as both the characters x
and f
are not present in the string educative.io
.
We show the output of all these operations using the Println()
method of the fmt
package.
The program prints the output given below and exits:
educative.io i true
educative.io xf false