Q:

Golang program to demonstrate the strings.ContainsAny() function

belongs to collection: Golang String Programs

0

In this program, we will create some strings and then check specified string contains any given character using ContainsAny() function. The ContainsAny() function returns Boolean value to the calling function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate the strings.ContainsAny() function is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate the
// strings.ContainsAny() function

package main

import "fmt"
import "strings"

func main() {
	str1 := "india"
	str2 := "australia"
	str3 := "america"
	str4 := "canada"

	fmt.Println(strings.ContainsAny(str1, "iu"))
	fmt.Println(strings.ContainsAny(str2, "iu"))
	fmt.Println(strings.ContainsAny(str3, "iu"))
	fmt.Println(strings.ContainsAny(str4, "iu"))
}

Output:

true
true
true
false

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created four string variables str1str2str3str4. After that, we checked specified characters in the specified string using ContainsAny() function. If anyone character is contained within the specified string then the ContainsAny() function returns true otherwise it will return false.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang program to check a specified string contain... >>
<< Golang program to demonstrate the strings.Compare(...